tags:

views:

48

answers:

4

Ok, so...i've a button with onclick.

<input id='post-favorite' onclick='Post.Favorite(15,"set");' type='submit' value='Favorite'>

but i need to change the onclick value when it's clicked to Post.Favorite(15,"unset"); there is a way to do that?

Because i've read in some places that is inpossible, intead use onclick use jquery click event.

But the problem is...if i use click event how do i send the values to the event or change them simulating the change of the onclick ?

Also the button is generated by php. If the user already has favorited the image if displays "unset" in the onclick, and if not favorited displays "set" to then when the user do the click set or not the image.

+2  A: 
Gaby
Explanation updated. Also the id could vary...i dont se hoy i can get the id...
Sein Kraft
do you have multiple of these buttons in your page or just one ? Also could you not figure is something is set/unset inside the `Post.Favorite` function and toggle it there ?
Gaby
I've just one button, and i need that deppending if the image is setted or not do the opposite but also need to get the image id...if i don't get the id i cannot know wich image add to favorites or remove.
Sein Kraft
@Sein, see updated answer for an alternative solution that fits your needs..
Gaby
@Sein my super simple solution has been updated too :) Take a look.
galambalazs
+1  A: 

Javascript

var PostFavorite = (function(_state) {
  return function(id, state) {
    _state = _state || state;
    Post.Favorite(id, _state);
    _state = (_state == "set") ? "unset" : "set";
  };
})();

HTML

<input onclick='PostFavorite(15,"set");' id='post-favorite' type='submit' value='Favorite'>

Note: the only thing you've changed in your HTML is that you've removed a dot. How cool is that? :)

galambalazs
A: 

If you want to remove the inline Javascript, you can keep the data in a special attribute and then rely on jQuery for click event handling.

http://api.jquery.com/jQuery.data/

Now you can store the data value with the element, change it and use a simple click event handler to automate it.

AndrewDotHay
A: 

Some other suggestion:

var click_action = 'Post.Favorite(15,"set");'
$("#post-favorite").click(function() { eval(click_action); });

You can put some other code in click_action varible, then it will be executed on click action.

Riateche
no, just no, no one more time.
redsquare