How would you set the value in a form input field with javascript. I tried by my own but it is not working. can anyone correct it. Incorrect Coding: <script type="text/javascript"> var val = document.getElementById("text"); val.value = "some input"; </script> <input type="text" id="text">
As you have seen in the demo, the text field named “mytext” has a default value of 12 when the form loads, which can be changed either by directly entering the value in the text input field, or by adjusting the value through the two javascript buttons labeled “+” or “-” .
Might help if you understood why it works. In your original you were running the script before the element existed on the DOM -- source order is VERY important when it comes to manipulating elements as they are run inline with the DOM being built. A good rule of thumb is to wait until right before </body> to run your scripts so the complete DOM is built before you start trying to manipulate things.
The following code works perfectly to auto fill drop down and linking functionality popping up a new tab and focusing it and everything. BUT, where I cannot get the text in the box to reset or set to anything. I followed your example, but it just doesn't seem to work. Any ideas? It just ends up leaving the link in the text box rather than the text I specify. <script type="text/javascript"> function newTab(theLocation){ var tab=window.open(theLocation,'_blank'); tab.focus(); } jQuery(document).ready(function() { $('#mainsearch').autocomplete({ source: function(request, response) { $.ajax({ url: "/_search.php", dataType: "json", data: { term : request.term }, success: function(data) { response(data); } }); }, select: function( event, ui ) { newTab(ui.item.value); document.getElementById("mainsearch").value='adsf'; }, minLength: 2, delay: 1000 }); }); </script> <input id="mainsearch" type="text" placeholder=" Search..." />