views:

166

answers:

1
+1  Q: 

jquery bind event

html:

<ul>
    <li><input type="submit" id="myId" value="someVal"/>
</ul>

jQuery

$('ul').find('input[type="submit"]').click(function{
         alert('nasty alert')
         $(this).attr('id','newId');
});

$('input#newId').click(function(){
          $(this).hide();
});

ok, so my intention is to change the id after one click and then the button to do something else(hide). i tried with live() too. In firebug it looks like the id has changed but my second click on the button triggers the same alert('nasty alert'). And something strange...if i use live(), on mouse right click the button dissapears (like it should). any sugestions? thanks

+2  A: 

You're basically attaching a click event handler twice to the same input.

There's no reason you should attach two event handlers, I've updated the code so a variable is used to keep track.

Edit: Fixed syntax error and now it's using .data

<ul>
<form>
<input type=submit value=go>
</form>
</ul>
<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js&gt;&lt;/script&gt;
<script>


    $('ul').find('input[type="submit"]').click(function() {
             if ( !$(this).data('_clicked') ) {
                 alert('nasty alert')
                 $(this).attr('id','newId');
                 $(this).data('_clicked', true);
             } else {
                 $(this).hide();
             }
             return false;
    });
</script>
meder
hmm, theoretically should work but it doesn't, now triggers just the hide() method
kmunky
Do you have a demo?
meder
nope, my code is much much complicated but i simplified it just to get the idea
kmunky
If you're dealing with multiple then you'll need a unique variable for each of them, you could use `.data` here
meder
oh.. and the `function` needs `()`
meder
there are just this two events, the first one implies ajax and submits a form and then if i click more on the button i want to update the mysql row not to add one more like the first click
kmunky
no problem with that (), i applied the idea not copy/paste your code
kmunky
hmm...let me try one more time, i think i left the return false; in the if(){.....etc.... return false;} :D (remains from my original code)
kmunky
Did you try with the updated example?
meder
i'll try, but it;s important to me to understand it. i have no idea about that .data and _clicked
kmunky
ok, now it really works, but can you explain a little bit the code or can you give me some good references about data and clicked, or what should i read to understand those? many thanks
kmunky