test
test

Reputation: 2466

jQuery show current html element value

Here is the code:

html:

<div class="toggle">
    <a href="#" class="active">5</a>
    <a href="#" class="">10</a>
    <a href="#" class="">25</a>
    <a href="#" class="">50</a>
</div>

<span class="result"></span>

js:

$('.toggle a').click(function(){
    $('.toggle a').removeClass('active');
    $(this).closest('a').addClass('active');
    $('.result').html('???');

});

css:

.active {
    color: red;}

How can i show when I press 'a' it will show current 'a's number.

Upvotes: 1

Views: 271

Answers (4)

Shyju
Shyju

Reputation: 218722

$(function(){

$('.toggle a').click(function(){
    var itemText=$(this).text();
    $('.toggle a').removeClass('active');
    $(this).closest('a').addClass('active');
    $('.result').html(itemText);

});        
})​

working sample http://jsfiddle.net/dvsrT/4/

Upvotes: 0

StrongLucky
StrongLucky

Reputation: 588

$('.result').html( $(this).html() );

Upvotes: 0

d_inevitable
d_inevitable

Reputation: 4451

$(this).text() will get you the contents of the tag that triggered the click.

$('.toggle a').click(function(){
    $('.toggle a').removeClass('active');
    $(this).closest('a').addClass('active');
    $('.result').html($(this).text());

});

Upvotes: 2

Simon Edstr&#246;m
Simon Edstr&#246;m

Reputation: 6619

I think this will do the trick for you:

$('.toggle a').click(function(){
    $('.toggle a').removeClass('active');
    $(this).closest('a').addClass('active');
    $('.result').html($(this).text()); // The new line
});

What you need to do is to retrive the text in the "a" that was clicked. This can be done by calling the text() metod on this object.

Upvotes: 2

Related Questions