2

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.

3
  • 1
    getElementsByName() ??? 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. 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>. Commented Feb 3, 2016 at 7:13

5 Answers 5

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); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

ohh, thnks... it works. bt what is [0], without it i can't get output
[0] is the index number. getElementsByName returns a list of elements. Let's suppose you had several inputs with the name 'text1' then [0] is the first input found, [1] the second input found, [2] the third input found etc
1

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" />

Comments

0

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).

1 Comment

name of my input feild
0

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()">

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.