ultrus Posted January 9, 2008 Share Posted January 9, 2008 Hello, I'm trying to access the id attribute of a parent <div> of a link. I'm not quite sure where to start. Here is one of my unsuccessful attempts to do this: <div id="success"> <a href="javascript:alert(node.nodeParent.getAttribute('id'));">Will we succeed?</a> </div> Is this possible? Any clues? Thanks for the assist. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 9, 2008 Share Posted January 9, 2008 you should be able to say "this.parentNode.id" Quote Link to comment Share on other sites More sharing options...
ultrus Posted January 9, 2008 Author Share Posted January 9, 2008 Hello emehrkay, Thank you for the response. Using this.parentNode.id returns an error, "this.parentNode has no properties". Below is the full page/script. Am I missing anything?: <html> <head> <title>JavaScript Trail to Success!</title> </head> <body> <div id="success"> <a href="javascript:alert(this.parentNode.id);">Will we succeed?</a> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 9, 2008 Share Posted January 9, 2008 Well there are a few problems with your code you're using inline js, which i recommend against, but you seem new so we'll let it slide you're using href="javascipt:" that makes the scope the window and not the element Easy fix would be to make the link look like this <a href="#" onclick="alert(this.parentNode.id); return false;">Will we succeed?</a> The better/more accessible/whatever fix would be to make your code look like this <html> <head> <title>JavaScript Trail to Success!</title> <script type="text/javascript"> onload = function(){ var link = document.getElementById('link'); link.onclick = function(){ alert(this.parentNode.id); return false; }; }; </script> </head> <body> <div id="success"> <a href="#" id="link">Will we succeed?</a> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
ultrus Posted January 9, 2008 Author Share Posted January 9, 2008 emehrkay, Sweet. That should work. Thanks much! The code I placed is the start of some more complicated content I don't have full control over. Divs will be added and removed using JavaScrpt, with external static content (for the most part), being loaded into the divs when added. For example: <div id="success1"> <input id="oneLiner" type="text" size="60"><br> <a href="#" onclick="alert(this.parentNode.id); return false;">Will we succeed?</a> </div> <div id="success2"> <input id="oneLiner" type="text" size="60"><br> <a href="#" onclick="alert(this.parentNode.id); return false;">Will we succeed?</a> </div> <div id="success3"> <input id="oneLiner" type="text" size="60"><br> <a href="#" onclick="alert(this.parentNode.id); return false;">Will we succeed?</a> </div> The way I'm getting this to work now, is passing the id get variable to an external php file, resulting in something like this: <div id="success3"> <input id="oneLiner" type="text" size="60"><br> <a href="#" onclick="alert('success3'); return false;">Will we succeed?</a> </div> Perhaps I should just stick with this if using inline js is a bad idea. I'll look more into this topic. Thanks. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 9, 2008 Share Posted January 9, 2008 If it is easier to use the inline js, do so. The *ideal* way is not to. If you check the source of some of the best web applications out there - google docs, gmail, yahoo mail, etc - you'll see a lot of inline calls. inline has benefits, but the new cool thing to do is not to use it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.