Denise
Denise

Reputation: 1469

jquery change different text in divs

Hallo,

I have three different divs and a paragraph in each one of them.

I would like to click on div 1 and text 1 changes - if I click on div 2, text 2 changes while text 1 is changing back to normal...

Just the text in the div where I just clicked should change. Since I am a newbie to jquery I can just achieve that all the texts are changing, but not the single one.

Who can help me?

Thanks!

Upvotes: 0

Views: 196

Answers (1)

Billy Moon
Billy Moon

Reputation: 58619

This will do the trick - first populate data for each div with the original content, then bind click event that sets everything back to original data, and sets itself to new text:

http://jsfiddle.net/billymoon/zAhJv/

$('div').each(function(){
    $(this).data({original:$(this).text()})  
})
$('div').click(function(){
    $('div').each(function(){
        $(this).text($(this).data('original'))
    })
    $(this).text('new text')
})

Upvotes: 3

Related Questions