Solved

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

Posted on 2004-04-27
5
31,873 Views
Last Modified: 2012-06-21
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
Comment
Question by:Samm1502
5 Comments
 
LVL 3

Accepted Solution

by:
ragerino earned 45 total points
ID: 10926780
try

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

greets
ragerino
0
 
LVL 30

Assisted Solution

by:third
third earned 40 total points
ID: 10926867
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
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 40 total points
ID: 10926899
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
 

Author Comment

by:Samm1502
ID: 10927248
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
 

Expert Comment

by:PeterSkold
ID: 20884227
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

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBScript on Html 15 47
iframe - style elements within an iframe 7 36
jQuery force form POST 7 45
ASP.NET Content Page 3 25
This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question