?
Solved

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

Posted on 2004-04-27
5
Medium Priority
?
31,891 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 3

Accepted Solution

by:
ragerino earned 180 total points
ID: 10926780
try

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

greets
ragerino
0
 
LVL 30

Assisted Solution

by:third
third earned 160 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 160 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
Suggested Courses

765 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