Reputation: 2466
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
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
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
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