Solved

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

Posted on 2004-04-27
5
31,866 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now