views:

41

answers:

4

The setting is easy; I want to be able to add a class to (in this case) a button when onClick-event is fired. My problem is that I haven't found a way to pass the button itself as the parameter to the function. I'd like to do something like:

<asp:Button ID="Button" runat="server" onclick="addClassByClick(this)"/>

And then a javaScript function something like this:

function addClassByClick(button){
    button.addClass("active")
}

I know I've got a lot of errors here, but that's why I'm posting. I've tried different scenarios with jQuery and without jQuery but I always end up with a broken solution (clicks suddenly stop coming through, class not added etc etc) so I decided to ask the pro's.

Any suggestion to what I can try? Thanks for reading *edit*and all the help!

+1  A: 

It needs to be a jQuery element to use .addClass(), so it needs to be wrapped in $() like this:

function addClassByClick(button){
  $(button).addClass("active")
}

A better overall solution would be unobtrusive script, for example:

<asp:Button ID="Button" runat="server" class="clickable"/>

Then in jquery:

$(function() {                       //run when the DOM is ready
  $(".clickabe").click(function() {  //use a class, since your ID gets mangled
    $(this).addClass("active");      //add the class to the clicked element
  });
});
Nick Craver
in your first example, would I actually be able to use "this" to pass the button as an argument to the function? Wouldn't I need to do some casting or is all that handled?
Phil
@Phil - Yes you can pass `this` in the first example, it's a DOM element, that's all that's needed :) I do urge you to use an unobtrusive option here like the second half of the answer though, it's much easier to maintain.
Nick Craver
@Nick - I will certainly try, but I'm really not comfortable with jQuery syntax yet.. I really should read up on it. Like in your example; if the button's classes are: CssClass="button horizontal floating" how would I target that?
Phil
@Phil - You could use any of them you wanted, for example `$(".button")` would work, or `$(".horizontal")` or a combination `$(".button.floating")` would only match an element with *both* classes. I would go here and click tutorials: http://docs.jquery.com/Main_Page (their database *just* started erroring, it should be fixed shortly).
Nick Craver
@Nick - Thanks, I really appreciate the help.
Phil
@Phil - Update, the site issues have been resolved, check out the selector tutorial here: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Find_me:_Using_selectors_and_events
Nick Craver
+2  A: 

Using jQuery:

$('#Button').click(function(){
    $(this).addClass("active");
});

This way, you don't have to pollute your HTML markup with onclick handlers.

piquadrat
The problem with this approach is that the element ID isn't guaranteed to be `Button`, it usually isn't.
Nick Craver
Well, you can only work with what the question gives you. I'm sure Phil is bright enough to look up the jQuery selector docs and find himself a nice selector apt for the job.
piquadrat
I wouldn't be so sure ;)
Phil
@piquadrat - The question does give you what's needed, the markup is ASP.Net markup :)
Nick Craver
A: 

$('#button').click(function(){ $(this).addClass('active'); });

kevtrout
A: 
$(document).ready(function() {
    $('#Button').click(function() {
        $(this).addClass('active');
    });
});

should do the trick. unless you're loading the button with ajax. In which case you could do:

$('#Button').live('click', function() {...

Also remember not to use the same id more than once in your html code.

Arnar Yngvason