views:

302

answers:

1

I'm trying to write a Greasemonkey script to update the onclick value of a bunch of links on a page. The HTML looks like this:

<a onclick="OpenGenericPopup('url-99.asp','popup',500,500,false)" href="javascript:void(0)">text</a>

I need to update the url-99.asp part of the Javascript into something like urlB-99.asp. In my script, I'm collecting all the links with an XPath expression and iterating through them:

var allEl = document.evaluate(
  'td[@class="my-class"]/a',
  document,
  null,
  XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  null);

for (var i = 0; i < allEl.snapshotLength; i++) {
  var el = allEl.snapshotItem(i);
  //something here;
}

If I try to alert(el.onclick), I get an error in the console:

Component is not available

I've read up on unsafeWindow and other Greasemonkey features, and I understand how to set an event handler for that link with a new onClick event, but how do I read the current onclick value into a string so I can manipulate it and update the element?

A: 

have you tried el.getAttribute("onclick") ?

and use //td[@class="my-class"]/a instead

w35l3y
I had completely forgotten I could retrieve and set attributes with `el.getAttribute("onclick")` and `el.setAttribute("onclick", "new text");`. Thanks!
Devin McCabe