0

I want to get the value of an input in a div. I've tried document.getElementById('id').getElementsByTagName('input') but somehow I can't get the value.

HTML:

<div id="ST_RTM1_352X-MESSAGE_TYPED01_TXT">
    <input class="MobileEditDisabled ABLED" value="E" size="2" maxlength="2" type="text">
</div>

JS :

var getV = document.getElementById("ST_RTM1_352X-MESSAGE_TYPED01_TXT").getElementsByTagName('input');
alert(getV.value);

JSFiddle: http://jsfiddle.net/aldimeola1122/2aLn33hn/

1
  • getElement s ByTagName. ElementSSSSSS. Plural. Commented Jan 21, 2015 at 15:18

6 Answers 6

2

Or be more clever:

var getV = document.querySelector("#ST_RTM1_352X-MESSAGE_TYPED01_TXT input");
alert(getV.value);
Sign up to request clarification or add additional context in comments.

1 Comment

Useful answer (aside from the snide intro)
1

getV contains a collection of nodes. You need to get first element getV[0] or getV.item(0) and then do another operations.

Comments

1

.getElementsByTagName returns a collection - reference the index you want:

var getV = document.getElementById("ST_RTM1_352X-MESSAGE_TYPED01_TXT").getElementsByTagName('input')[0];

Comments

1

You forgot de position because getV will get a collention's nodes, use [0] to get the node:

var getV = document.getElementById("ST_RTM1_352X-MESSAGE_TYPED01_TXT").getElementsByTagName('input')[0];

DEMO LIVE

Comments

1

Just add [0] after getElementsByTagName('input').

Comments

1

A Solution...

Lets say it like this: My Version takes the Div Element and searches for All Input child Elements. It works ;)

var getV = document.getElementById("ST_RTM1_352X-
MESSAGE_TYPED01_TXT").getElementsByTagName('input');
alert("Inputelements in the Div:" +getV.length);
for (var i = 0; i<getV.length; i++){
    alert("Value of input " + i + ": " + getV[i].value);   
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.