views:

92

answers:

4
<a href="" onclick="runstuff();return false;">Item</a>

After executing runstuff(), I want to use JQuery to make Item bold. Will I need an "id"? What about "self"?

+6  A: 

With jQuery, you ought to bind to an attribute or class on the link itself. For instance:

<a href="foo.html" class="makeBold">Click Me</a>
$("a.makeBold").click(function(e){
  e.preventDefault();
  $(this).css("font-weight", "bold");
});

Of course, that's a CSS solution. You can allso wrap the element in <b> or <strong> tags:

$("a.makeBold").click(function(e){
  e.preventDefault();
  $(this).wrap("<strong>");
});
Jonathan Sampson
It'd also be a good idea to specify a specific set of links by scoping the CSS selectors (ie: `#boldlinks a` or what have you).
Damien Wilson
You forgot to call runstuff()! +1 though
elwyn
@elwyn: He didn't forget. He simply didn't reference `runstuff()` as `runstuff()` It was defined as the handler functions sent to the `click` event handler. Does the same thing, but you don't need to name the function if you don't want. =D
Jeff Rupert
+1  A: 

The A you clicked will be 'this' in your example, so just define runstuff:

function runstuff() {
    jQuery(this).css("font-weight", "bold");
}
darkporter
A: 

I don't know if it would bolden the text before or after runstuff() ran, but I would expect this would work:

$('a[onclick^=runstuff]').click(function(){
      $(this).css("font-weight", "bold");
});

Perhaps you could ensure it would run after runstuff() by using jQuery to change the onclick attribute? Something like

$('a[onclick^=runstuff]').attrib('onclick','runstuff();boldtext();return true;');

Then you'd have to define boldtext()

Charles
A: 

You can try to modify your HTML into:

<a href="#" onclick="runstuff(this);return false;">Item</a>

And, you can have the script like:

function runstuff(link) {
    jQuery(link).css("font-weight", "bold");
}
Ferry