开发者

JSP, Javascript, getting ElementId within another Id

my question tod开发者_如何学Cay 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.


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');


What you want is

document.getElementById ('whatIwantToGetTo').parentNode


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜