- How to obtain text from a textbox in Javascript
- javascript getting the value of a text box
- How do I get the value of text input field using JavaScript?
- 16 Answers 16
- Method 1
- For example
- Method 2
- For example
- Method 3
- For example
- Method 4
- For example
- Method 5
- For example
- Method 6
- For example
- How to get value from HTML textbox in Javascript
- 5 Answers 5
- How to get text box value in JavaScript
- 16 Answers 16
How to obtain text from a textbox in Javascript
UPDATE
Ok, after trying «.value» for god knows x amount of time again, it some how worked. Problem solved. Thanks a lot for those who has responded.
UPDATE How can I obtain the text(strings) from my form on my HTML page with Javascript? I just want to take whatever was inputted into the text box and write into my XML file.
For example, if I type «HELLO» into the text box, I want «HELLO» written in the XML file. I have tried both «.value» and «.text», they don’t work. Also tried «.textcontent», no good too. I’m thinking that I need to use a different code. Here’s an image of what I’m trying to do:
http://img94.imageshack.us/img94/5677/sdfsdfsdfs.jpg Here are the files if you want to mess with them personally:
http://www.mediafire.com/?e29t6ipatsqun70 Here’s my HTML file:
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); var fso = new ActiveXObject("Scripting.FileSystemObject"); var FILENAME = 'G:\\CST2309 - Web Programming 1\\Copy of Take Home Exam - Copy\\PersonXML2.xml'; function SaveXML(UserData) < var usrn = document.getElementById("Username").text; var pswd = document.getElementById("Pswd").text; var pid = document.getElementById("PersonID").text; var fname = document.getElementById("FirstName").text; //This is where I'm having trouble with var lname = document.getElementById("LastName").text; var gender = document.getElementById("Gender").text; var dob = document.getElementById("DOB").text; var title = document.getElementById("Title").text; var file = fso.CreateTextFile(FILENAME, true); file.WriteLine('\n'); file.WriteLine('\n'); for (countr = 0; countr \n'); > // end for countr file.Write(' \n'); file.WriteLine(' \n'); file.Close(); > // end SaveXML function -------------------- function LoadXML(xmlFile) < xmlDoc.load(xmlFile); return xmlDoc.documentElement; >//end function LoadXML() function initialize_array() < var person = new Array(); var noFile = true; var xmlObj; if (fso.FileExists(FILENAME)) < xmlObj = LoadXML(FILENAME); noFile = false; >// if else < xmlObj = LoadXML("PersonXML.xml"); //alert("local" + xmlObj); >// end if var usrCount = 0; while (usrCount < xmlObj.childNodes.length) < var tmpUsrs = new Array(xmlObj.childNodes(usrCount).getAttribute("Usrname"), xmlObj.childNodes(usrCount).getAttribute("Pswd"), xmlObj.childNodes(usrCount).getAttribute("PersonID"), xmlObj.childNodes(usrCount).getAttribute("FirstName"), xmlObj.childNodes(usrCount).getAttribute("LastName"), xmlObj.childNodes(usrCount).getAttribute("Gender"), xmlObj.childNodes(usrCount).getAttribute("DOB"), xmlObj.childNodes(usrCount).getAttribute("Title")); person.push(tmpUsrs); usrCount++; >//end while if (noFile == false) fso.DeleteFile(FILENAME); SaveXML(person); > // end function initialize_array()
javascript getting the value of a text box
Please avoid mixing JavaScript and HTML. You can remove onclick attribute and replace it with this in plain JavaScript somewhere after the DOM has loaded:
document.getElementById('btnSearch').onclick = function() < var search = document.getElementById('search').value; var searchEncoded = encodeURIComponent(search); window.location.url = "http://www.website.com/search/" + searchEncoded; >
Also remember about escaping the search box, e.g. using encodeURIComponent() . Here is a working jsfiddle example.
onclick="location.href='http://www.website.com/search/'+document.getElementById('search').value;"
But I wouldn’t ever write that in one of my project as writing script directly on tags is a bad practice.
then why are you posting this as an answer? It’s best, in my opinion, to try to discourage bad practices whenever possible
It might be someone that is still learning about JavaScript. Best practices should be followed, but are not mandatory.
I moved the event handler out of the button as it is more maintainable. Also I encode the search query so that it gets to the server properly.
var search = document.getElementById('search'); var submit = document.getElementById('btnSearch'); submit.addEventListener('click', function(e) < var searchValue = encodeURIComponent(search.value); // encode the search query window.location.href = 'http://www.website.com/search/' + searchValue ; >);
You can add it to the onclick event like so
document.getEelementById("btnSearch").onclick = function() < location.href='http://www.website.com/search/' + document.getEelementById("search").value; >
edit: aaaaand too slow. oh well. At least this is not inline.
You would be better off using the < script>tag for this task. Example:
.
However, you should try to ‘clean’ a little the text from the textbox so when you append it to the url you get a valid url. You should trim the text, then search for special characters and escape them, etc.
How do I get the value of text input field using JavaScript?
I am working on a search with JavaScript. I would use a form, but it messes up something else on my page. I have this input text field:
16 Answers 16
There are various methods to get an input textbox value directly (without wrapping the input element inside a form element):
Method 1
document.getElementById(‘textbox_id’).value to get the value of desired box
For example
Note: Method 2,3,4 and 6 returns a collection of elements, so use [whole_number] to get the desired occurrence. For the first element, use [0] , for the second one use [1] , and so on.
Method 2
Use document.getElementsByClassName(‘class_name’)[whole_number].value which returns a Live HTMLCollection
For example
document.getElementsByClassName(«searchField»)[0].value; if this is the first textbox in your page.
Method 3
Use document.getElementsByTagName(‘tag_name’)[whole_number].value which also returns a live HTMLCollection
For example
document.getElementsByTagName(«input»)[0].value; , if this is the first textbox in your page.
Method 4
document.getElementsByName(‘name’)[whole_number].value which also >returns a live NodeList
For example
document.getElementsByName(«searchTxt»)[0].value; if this is the first textbox with name ‘searchtext’ in your page.
Method 5
Use the powerful document.querySelector(‘selector’).value which uses a CSS selector to select the element
For example
- document.querySelector(‘#searchTxt’).value; selected by id
- document.querySelector(‘.searchField’).value; selected by class
- document.querySelector(‘input’).value; selected by tagname
- document.querySelector(‘[name=»searchTxt»]’).value; selected by name
Method 6
document.querySelectorAll(‘selector’)[whole_number].value which also uses a CSS selector to select elements, but it returns all elements with that selector as a static Nodelist.
For example
- document.querySelectorAll(‘#searchTxt’)[0].value; selected by id
- document.querySelectorAll(‘.searchField’)[0].value; selected by class
- document.querySelectorAll(‘input’)[0].value; selected by tagname
- document.querySelectorAll(‘[name=»searchTxt»]’)[0].value; selected by name
Browser | Method1 | Method2 | Method3 | Method4 | Method5/6 |
---|---|---|---|---|---|
IE6 | Y(Buggy) | N | Y | Y(Buggy) | N |
IE7 | Y(Buggy) | N | Y | Y(Buggy) | N |
IE8 | Y | N | Y | Y(Buggy) | Y |
IE9 | Y | Y | Y | Y(Buggy) | Y |
IE10 | Y | Y | Y | Y | Y |
FF3.0 | Y | Y | Y | Y | N IE=Internet Explorer |
FF3.5/FF3.6 | Y | Y | Y | Y | Y FF=Mozilla Firefox |
FF4b1 | Y | Y | Y | Y | Y GC=Google Chrome |
GC4/GC5 | Y | Y | Y | Y | Y Y=YES,N=NO |
Safari4/Safari5 | Y | Y | Y | Y | Y |
Opera10.10/ | |||||
Opera10.53/ | Y | Y | Y | Y(Buggy) | Y |
Opera10.60 | |||||
Opera 12 | Y | Y | Y | Y | Y |
Useful links
How to get value from HTML textbox in Javascript
I simply want to have a textbox on my webpage, using the HTML form, and input tags, and be able to have the inputted value be used by the Javascript on the page. My HTML looks like this:
var topMenuChoice = document.getElementById("firstinput"); document.write(topMenuChoice); >
However, all I see on the webpage, underneath the textbox, is «[object HTMLInputElement]». What do I do to get this to work right? Thanks
5 Answers 5
here’s an example with change event listener for firing a function when there’s a change in form
var div = document.querySelector('div'); var topMenuChoice = document.getElementById("firstinput"); topMenuChoice.addEventListener('change',function(e)< div.innerHTML = e.target.value/***e.target.value is your input***/ var divInner = div.innerHTML; setTimeout(function()< document.write(divInner); >,2000) >)
This seems to be in the right direction, now, how do I go about assigning the value to a variable? To be used in math problems
@ethanzh it depends where you want to show the input value; in my updated example I’m setting innerHTML property on another div but there are many ways you could display it ; using document.write will rewrite your markup (deleting what was there before)
document.write(document.forms['firstbox'].firstinput.value);
var topMenuChoice = document.getElementById("firstinput"); document.write(topMenuChoice.value); >
var htmlInputElementObjet = document.getElementById("firstinput"); document.write(htmlInputElementObjet.value);
If you want to get the text typed in your input you need to use the value property of the element. You can also use another HTML tag to show the results (avoid using document.write):
var topMenuChoice = document.getElementById("firstinput"); document.getElementById("result").innerHTML = topMenuChoice.value;
You have to consider the usage of an event (click, keypress) to control the exactly moment to retrieve the input value.
document.getElementById('firstinput').addEventListener('keypress', function(e) < if(e.which == 13) < //detect enter key pressed e.preventDefault(); document.getElementById('result').innerHTML = this.value; >>);
How to get text box value in JavaScript
I am trying to use JavaScript to get the value from an HTML text box but value is not coming after white space For example:
var jobValue = document.getElementById('txtJob').value
16 Answers 16
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:
+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.
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:
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);