Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-04-27
5
Medium Priority
?
31,896 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

604 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