0

I have this code:

<div class="pButton"><a href="javascript:;" onclick="parent.window.close()"></a>

Now, I cannot edit this code (I cannot add an ID or class to the link.. notice that the links calss is not 'pButton', 'pButton' is the div's class which the link is inside) and Without using Jquery, is there a way for me to change the onclick so that

onclick="javascript:OpenWindow('popup.htm');"

?

Note: I cannot use getElementById since I cannot give the link an ID or a class. I cannot do anything to the line:

<div class="pButton"><a href="javascript:;" onclick="parent.window.close()"></a>

Also, I am using IE8.

1

1 Answer 1

1

If only one element with the name pButton, or it is the first, try this:

document.getElementsByClassName('pButton')[0].children[0].onclick = "javascript:OpenWindow('popup.htm');"

IE8 support, use:

document.querySelectorAll('.pButton')[0].children[0].onclick = "javascript:OpenWindow('popup.htm');"

Older than IE8, ??, ...:

var all = context.getElementsByTagName("div");  /* for catching all div's */
for (i = 0; i < all.length; i++) {
    if (all[i].className && all[i].className == "pButton") {
        all[i].children[0].onclick = "javascript:OpenWindow('popup.htm');"
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Fixed your querySelector for you - you were looking for a <pButton> element :p
Also, note that .children[0] is more reliable than .childNodes[0], because even a simple new line between the start of the element and the target node will mess childNodes up.
updated. but is children[] supported in older IE, <= 8, ?
Yes, but in older IE comment nodes are counted as children too

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.