1

An anchor in my HTML has an onclick attribute like so:

 <a href="javascript:void(0);" onclick="javascript:OpenNewWindow('/help_options.asp?ID=23', 350, 250);" class="help_question_mark">?</a>

I'm trying to strip away the JavaScript handlers to get the bare URL and prop it to the href attribute so the result would look like this:

 <a href="/help_options.asp?ID=23" class="help_question_mark">?</a>

I figured it'd be simple, just write something like this:

$('a.help_question_mark').each(function(){
  help_question_mark_link = $(this).attr('onclick').split('javascript:OpenNewWindow(\'').join('').split('\', 350, 250);').join('');
  $(this).removeAttr('onclick');
  $(this).attr('href',help_question_mark_link);
});

In jQuery 1.1.1, this seems to work, but not in 1.4.2 which I am limited to using.

Can anybody shed some light on this, I'm in a pickle.

Here's a jsFiddle.

11
  • 1
    Maybe this will help explain what's going on: jsfiddle.net/wa5N5/1 basic debugging. Commented Dec 18, 2013 at 21:22
  • How exactly is it not working? Are there errors or is it just doing nothing? Commented Dec 18, 2013 at 21:26
  • @KevinB Not sure how that helps. Even more basic debugging is jsHint error Script URL that I do not know how to get around. Commented Dec 18, 2013 at 21:27
  • @SuperScript Nothing at all. It's breaking JavaScript altogether. Commented Dec 18, 2013 at 21:27
  • @henryaaron my point is the string you are getting from .attr() isn't what you think it is. that's why the .split doesn't work the way you expect it to. i suggest cross-browser testing it too, you may get different results based on browser. Commented Dec 18, 2013 at 21:28

1 Answer 1

2

OK. So after much fiddling I discovered that jQuery 1.4.2 returns a special onclick object when you call $(...).attr('onclick'), not just a string.

So we need to break away from jQuery use this:

this.getAttribute('onclick')

instead of this:

$(this).attr('onclick')

Just plain weird. See this JSFiddle.

P.S. I found this out by console.loging the value returned by $(...).attr('onclick').

Sign up to request clarification or add additional context in comments.

2 Comments

And this is likely due to jquery retrieving properties rather than attributes with .attr() in older versions.
That's interesting, didn't take much out of that console.log, it all look pretty normal to me. Much obliged

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.