4

What I am attempting to do is is access hidden object in a div. What happends is a user will click on button that will then perform some task such as delete a user. This may be easier if I show what I have.

<div class="mgLine">
    <input type="hidden" name="tenentID" value="1">
    <p class="mgName">James Gear</p>
    <input type="text" class="mgBill" value="" placeholder="Post Bill Link Here">
    <a href="#" class="mgButton" onclick="alertTest(this)">Submit Bill</a>
    <a href="#" class="mgNP">Not Paid</a>
    <a href="#" class="mgButton">Change Password</a>
    <a href="#" class="mgButton">Delete User</a>
</div>

What I want the system to do is alert the value of one which it gets from the hidden field when the "submit bill" is pressed.

function alertTest(e){
//e.parentNode
window.alert(e.parentNode.getElementsByTagName("tenentID")[0]);
}

I am attempting to use JavaScript DOM to access the element. I hope this made at least some sense. There will be many of these entries on the page.

2
  • use getElementsByName instead of getElementsByTagName Commented May 29, 2013 at 23:10
  • Always the first child? If so, e.parentNode.children[0] Commented May 29, 2013 at 23:14

5 Answers 5

6

You need to use getElementsByName instead of getElementsByTagName

function alertTest(e){
//e.parentNode
window.alert(document.getElementsByName("tenentID")[0]);
}

getElementsByTagName is for selecting elements based on its tag say div, input etc..

getElementsByName

getElementsByTagName

Realizing that you might have multiple div section and your hidden input is the first child you could use these:-

e.parentNode.getElementsByTagName("input")[0].value;

or

e.parentNode.firstElementChild.value;

if it is not the firstCHild and you know the position then you could use

e.parentNode.children(n).value; //n is zero indexed here

Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

When I was looking into this, I was getting the error: Object #<HTMLDivElement> has no method 'getElementsByName' (I was using code identical to yours). However, document.getElementsByName("tenentID")[0] worked.
This looks like it will work for what I am trying to do. Thank you so much for your help and everyone else's!
6

The modern method would be to use querySelector.

e.parentNode.querySelector("[name=tenentID]");

http://jsfiddle.net/ExplosionPIlls/zU2Gh/

However you could also do it with some more manual DOM parsing:

var nodes = e.parentNode.getElementsByTagName("input"), x;
for (x = 0; x < nodes.length; x++) {
    if (nodes[x].name === "tenentID") {
        console.log(nodes[x]);
    }
}

http://jsfiddle.net/ExplosionPIlls/zU2Gh/1/

Comments

0

Try this:

function alertTest(e){
    alert(e.parentNode.getElementsByName("tenentID")[0].value);
}

Comments

0

I usually set an id attribute on the hidden element, then use getElementById.

<input type="hidden" id="tenentID" name="tenentID" value="1">

then I can use...

var tenentValue = document.getElementById("tenentID").value;

1 Comment

What if there are several of them? Seems that's likely the case here.
0

In general, your best bet for accessing a specific element is to give it an ID and then use getElementById().

function alertTest(ID){
  alert(document.getElementById(ID).value);
}

Names can be duplicated on a page, but the ids have to be unique.

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.