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 can't find anything with the given ID, but this is a bug.
i am getting only "software".
id-vs-name won't affect this; I suspect what's happened is that (contrary to the example code) you've forgotten to quote your ‘value’ attribute:
<input type="text" name="txtJob" value=software engineer>
var word = document.getElementById("word").value;//by id
or
var word = document.forms[0].elements[0].value;//by index
//word = a word from form input
var kodlandi = escape(word);//apply url encoding
alert(escape(word));
or
alert(kodlandi);
the problem you are not using encoding for input values from form so not browser adds ones to ...
ontop has some problems as unicode encoding/decoding operations so use this function encoding strings/arrays
function urlencode( str )
{
// http://kevin.vanzonneveld.net3.
// + original by: Philip Peterson4.
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)5.
// * example 1: urlencode('Kevin van Zonneveld!');
// * returns 1: 'Kevin+van+Zonneveld%21'7.
var ret = str;
ret = ret.toString();
ret = encodeURIComponent(ret);
ret = ret.replace(/%20/g, '+');
return ret;
}
ex.
var word = "some word";
word = urlencode(word);
<!DOCTYPE html>
<html>
<body>
<label>Enter your Name here: </label><br>
<input type= text id="namehere" onchange="displayname()"><br>
<script>
function displayname() {
document.getElementById("demo").innerHTML =
document.getElementById("namehere").value;
}
</script>
<p id="demo"></p>
</body>
</html>