• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 31906
  • Last Modified:

How to get id value from an object rather than object from an id

I have a Javascript function that is passed an object corresponding to the <DIV> element that contains that last displayed submenu on the page.

What I need to do is obtain the id of this <div> object and then use it to obtain another object that has the id "lnk" + <id of aformentioned DIV object>

I tried using the syntax:


document.all["lnk" + myDivObj.id];

but this didn't work.

Any help much appreciated.
Thanks
Sam
0
Samm1502
Asked:
Samm1502
3 Solutions
 
ragerinoCommented:
try

var lnkobj = document.getElementById('lnk' + myDivObj.id);

greets
ragerino
0
 
thirdCommented:
try this,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script>
  function changeText(divID, flag){
    if(flag)
      document.getElementById('lnk'+divID).innerHTML = 'the mouse is over the div "' + divID + '"';
    else
        document.getElementById('lnk'+divID).innerHTML = '';
  }
</script>
</head>
<body>
<div id="id1" style="color:red" onmouseover="changeText(this.id, true)" onmouseout="changeText(this.id, false)">mouseover here</div>
<br>
<div id="lnkid1" onmouseover=""></div>
</body>
</html>
0
 
DanRollinsCommented:
It works if you spell everything correctly.  Here is an example:

<HTML><BODY>
<script>
function DoTest( oDiv ) {
      // alert( oDiv.id );
      var oLnk= document.all["lnk"+oDiv.id ];
      // alert( oLnk.id );
      oLnk.innerText="hi there";  // make a visible change for testing
}
</script>
<div id="MyDiv" onclick="DoTest(this);">this is the div Click IT!</div>
<a href="somedomain.com" id="lnkMyDiv">this is the link</a>
</body></html>
0
 
Samm1502Author Commented:
I have tried to be as fair as possible as all answers were great.  it turns out I was trying to get the ID of the wrong object.  I thought it was the div but it wasn't and didn't have an id assigned so wasn't working.

Thanks for all the help greatly appreciated.

Sam
0
 
PeterSkoldCommented:
Wondering how to get at a listing of all the values/properties of the <div>? .childNodes? The oDiv.id delivers the id ('MyDiv') of the object as seen above. But sometimes it would be useful to loop through all of them. Anyone know if and how to do this?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now