html - How to get value of another input field using javascript - Stack Overflow

How to find the value of text field using onblur() in next input field.I tried:function get_value() {va

How to find the value of text field using onblur() in next input field.

I tried:

function get_value() {

  var inv_nrs;
  inv_nrs = document.getElementsByTagName('text1').value;
  alert(inv_nrs);   
}

text1 is name of input which I am trying to get value.
text2 is name of input where onblur() is triggered.

How to find the value of text field using onblur() in next input field.

I tried:

function get_value() {

  var inv_nrs;
  inv_nrs = document.getElementsByTagName('text1').value;
  alert(inv_nrs);   
}

text1 is name of input which I am trying to get value.
text2 is name of input where onblur() is triggered.

Share Improve this question edited Feb 3, 2016 at 7:42 asked Feb 3, 2016 at 7:09 user5863306user5863306 3
  • 1 getElementsByName() ??? – Bhojendra Rauniyar Commented Feb 3, 2016 at 7:11
  • 1 getElementsByTagName returns a collection of elements, not a single element. You have to index it to access each element of the collection. – Barmar Commented Feb 3, 2016 at 7:12
  • 1 The same thing is true of getElementsByName, which is probably the function you want here. getElementsByTagName('text1') looks for elements like <text1>. – Barmar Commented Feb 3, 2016 at 7:13
Add a ment  | 

5 Answers 5

Reset to default 2

Two problems:

  1. To get elements by their name attribute, use document.getElementsByName(), not document.getElementsByTagName.
  2. Since these functions return a collection, not a single element, you have to index them to get a specific element.

So the function should be:

function get_value() {

    var inv_nrs;
    inv_nrs = document.getElementsByName('text1')[0].value;
    alert(inv_nrs); 
}

Here's a simple snippet which illustrates a way to do this.
(You may wish to use alert in place of console.log)

document.getElementById("text2").onblur = function() {
  console.log(document.getElementById("text1").value)
}
<input type="text" id="text1" value="123" />
<input type="text" id="text2" />

Are you looking for an element with id = "text1" or real name = "text1"? At least if it's their id try getElementById("text1"), that returns one single element. If you talking about the name-attribute, take getElementByName("text1"), this may return more than one element (if there are more then one with the same name).

i think you want this???

function get_value() 
{

var inv_nrs;
inv_nrs = document.getElementById('txt1').value;
 document.getElementById('txt2').value=inv_nrs;
}
<input type="text" id="txt1" >
<input type="text" id="txt2" onblur="get_value()">

If you search with tagname then you need to insert a tagname:

document.getElementsByTagName('input')[whole_number].value which also returns a live HTMLCollection

Eg. document.getElementsByTagName("input")[0].value; ,if this is the first textbox in your page.

You can get the value of an html element also on different ways:

document.getElementsByName('text1')[whole_number].value which also returns a live NodeList

Eg. document.getElementsByName("searchTsxt")[0].value; if this is the first textbox with name 'searchtext' in your page.

You can also get element by Id:

document.getElementById('IDHere').value to get the value of desired box

You can also get it by way of Classname:

Use document.getElementsByClassName('class_name')[whole_number].value which returns a Live HTMLCollection

Good luck

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745003875a4605656.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信