Gonçalo Vieira
Gonçalo Vieira

Reputation: 2249

JSP, Javascript, getting ElementId within another Id

my question today revolves arround the world of javascript in a Websphere JSP environment...

I have a code that is somewhat like this:

<div id="randomDynamicId">
    <input id="whatIwantToGetTo">
</div>

I know that I could just look for that id directly, but this is in a Websphere portal, I "should" be able to link to it directly by document.getElementById(), but I always need to acquire the id of the prior div.
(it ends up being the portlet id with the namespace and since sometimes these portlets might be replicated I want to target just one specifically)

Any way that I might be able to do this?
Thanks in advance.

Upvotes: 0

Views: 1285

Answers (3)

Gustav Barkefors
Gustav Barkefors

Reputation: 5086

As per your request I post my earlier comment as an answer, a little more elaborated: If the issue is that whatIwantToGetTo is not namespace-prefixed so that you end up with multiple elements with the same id on your page, you should rewrite your JSP to namespace all id attributes as well. This should probably be done anyway (if you can modify the HTML, that is), at the very least if there is a possibility of the portlet occurring more than once on any page!

However, seeing as you're on a WebSphere Portal 7, you most likely have Dojo around and you could leverage its CSS-style selector mechanism like so:

var inputElement = dojo.query('#randomId > input');

Upvotes: 1

Icarus
Icarus

Reputation: 63964

If you can use jQuery, you can accomplish this like so:

var parent = $('#whatIwantToGetTo').parent();

See Here

The pure javascript alternative is something like this: alert(document.getElementById('objectIWant').parentNode.id);

See here

Upvotes: 0

HBP
HBP

Reputation: 16043

What you want is

document.getElementById ('whatIwantToGetTo').parentNode

Upvotes: 0

Related Questions