Javier Mosceno
Javier Mosceno

Reputation: 15

How to use toggle?

<table border=2>
    <tr>
        <td class="here" one="lorem" two="ipsum"> click </td>
        <td class="here" one="aaa" two="bbb"> click </td>
    </tr>
</table>

$('.here').click(function(){
  $(this).html($(this).attr('one'));
})

http://jsfiddle.net/uzhru/

How can i modify this javascript for function toggle or other solution? I would like - if i click on TD then this show me attribute one, and if i next click then this show me attribute two, next one, next two etc

Upvotes: 0

Views: 72

Answers (2)

John Lawrence
John Lawrence

Reputation: 2923

ocanal's answer works perfectly but I thought I'd add that this can also be done with toggle which when given two functions will switch between them for each click:

$(".here").toggle(function() {
    $(this).html($(this).attr('one'));
}, function() {
    $(this).html($(this).attr('two'));
});

http://jsfiddle.net/7pUS4/

Upvotes: 1

Okan Kocyigit
Okan Kocyigit

Reputation: 13441

$('.here').click(function(){
  if($(this).html() == $(this).attr('one'))
    $(this).html($(this).attr('two'));
  else 
    $(this).html($(this).attr('one'));
})​

DEMO

Upvotes: 1

Related Questions