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.

SilentDev
  • 20,997
  • 28
  • 111
  • 214

1 Answers1

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');"
    }
}
Asons
  • 84,923
  • 12
  • 110
  • 165