Set Value of Input Using Javascript Function
Try...
for YUI
Dom.get("gadget_url").set("value","");
with normal Javascript
document.getElementById('gadget_url').value = '';
with JQuery
$("#gadget_url").val("");
Set the value of an input field
This is one way of doing it:
document.getElementById("nameofid").value = "My value";
Set value to input field onchange from other input field
Simply assign an identifier to each input, and pass the input to the function:
<input type="text" id="myInput1" onchange="myChangeFunction(this)" placeholder="type something then tab out" /><input type="text" id="myInput2" />
<script type="text/javascript"> function myChangeFunction(input1) { var input2 = document.getElementById('myInput2'); input2.value = input1.value; }</script>
Change value of input onchange?
You can't access your fieldname
as a global variable. Use document.getElementById:
function updateInput(ish){
document.getElementById("fieldname").value = ish;
}
and
onchange="updateInput(this.value)"
how to set the value of an html input field from a javascript function
inside the if condition, change the return to this:
document.getElementsByName("AMOUNT")[0].value = pair[1];
Input | Get the new value set by a JavaScript function into a Controller public function
SOLUTION AJAX CALL IN JS SCRIPT :
...
else if (d1 == 4 && d2 == 27 && d3 == '' && pl1 == 2 && pl2 == 2) { $('#productprice').val('59.00'); }
else if (d1 == 4 && d2 == 27 && d3 == '' && pl1 == 3 && pl2 == 3) { $('#productprice').val('69.00'); }
else if (d1 == 4 && d2 == 27 && d3 == '' && pl1 == 4 && pl2 == 4) { $('#productprice').val('79.00'); }
else if (d1 == 4 && d2 == 27 && d3 == '' && pl1 == 4 && pl2 == 5) { $('#productprice').val('89.00'); }
var rowID = rowid;
var orderID = $('#orderID').val();
var productprice_new = $('#productprice').val();
console.log(productprice_new,rowID,orderID);
$.ajax({
type: 'post',
url: '/assets/ajax/ajaxupdate.php',
data: {
'rowid' : rowid,
'orderID' : orderID,
'productprice_new' : productprice_new,
},
success: function (data) {
console.log('worked!');
},
error: function (data) {
console.log('Error:', data);
}
})
How do I get the value of text input field using JavaScript?
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
document.getElementById("searchTxt").value;
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
Usedocument.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 iddocument.querySelector('.searchField').value;
selected by classdocument.querySelector('input').value;
selected by tagnamedocument.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 iddocument.querySelectorAll('.searchField')[0].value;
selected by classdocument.querySelectorAll('input')[0].value;
selected by tagnamedocument.querySelectorAll('[name="searchTxt"]')[0].value;
selected by name
Support
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 |
Related Topics
Are Loops Really Faster in Reverse
Understanding Prototypal Inheritance in JavaScript
What Does the @ Mean Inside an Import Path
Get First and Last Date of Current Month with JavaScript or Jquery
Making Custom Right-Click Context Menus for My Web-App
What Is Ajax and How Does It Work
Sorting in JavaScript: Should Every Compare Function Have a "Return 0" Statement
Js:Convert Array of Strings to Array of Objects
"Object Doesn't Support This Property or Method" Ie10/11
Difference Between JSON.Stringify and JSON.Parse
What's the Cause of the Error 'Getaddrinfo Eai_Again'
How to Get the Day of Week and the Month of the Year
How to Insert Text into the Textarea at the Current Cursor Position