html – How to get text box value in JavaScript

html – How to get text box value in JavaScript

Your element does not have an ID but just a name. So you could either use getElementsByName() method to get a list of all elements with this name:

var jobValue = document.getElementsByName(txtJob)[0].value  // first element in DOM  (index 0) with name=txtJob

Or you assign an ID to the element:

<input type=text name=txtJob id=txtJob value=software engineer>

+1 Gumbo: ‘id’ is the easiest way to access page elements. IE (pre version 8) will return things with a matching ‘name’ if it cant find anything with the given ID, but this is a bug.

i am getting only software.

id-vs-name wont affect this; I suspect whats happened is that (contrary to the example code) youve forgotten to quote your ‘value’ attribute:

<input type=text name=txtJob value=software engineer>

html – How to get text box value in JavaScript

var word = document.getElementById(word).value;//by id
var word = document.forms[0].elements[0].value;//by index
//word = a word from form input
var kodlandi = escape(word);//apply url encoding


the problem you are not using encoding for input values from form
not browser adds ones to …

ontop has some problems as unicode encoding/decoding operations
use this function encoding strings/arrays

function urlencode( str ) 
// http://kevin.vanzonneveld.net3.    
// +   original by: Philip Peterson4.    
// +   improved by: Kevin van Zonneveld (    
// *     example 1: urlencode(Kevin van Zonneveld!);
// *     returns 1: Kevin+van+Zonneveld%217. 
   var ret = str; 
   ret = ret.toString();
   ret = encodeURIComponent(ret);
   ret = ret.replace(/%20/g, +);
   return ret;

var word = some word;
word = urlencode(word);

Leave a Reply

Your email address will not be published.