Reputation: 343
I have a span class with a link. I want to hide the span class when clicking on the link or the span class itself with the jQuery. Can someone please tell me how it can be done? Here's my code and demo.
Thanks
Upvotes: 0
Views: 2490
Reputation: 253308
Just a thought, but this also works:
$('a:has("span"), a > span').click(
function(){
$(this).find('span').remove();
});
jQuery API references:
Upvotes: 0
Reputation: 13966
$(document).ready( function() {
$('a.msg, a.entry').click( function() {
$(this).find('span').hide();
return false;
});
});
it is good to always follow best practices and include the tag with the class (for optimization) and return false with with function
Upvotes: 0
Reputation: 18452
To hide any span elements within the a:
$(document).ready( function() {
$('.msg, .entry').click( function() {
$(this).find('span').hide()
});
});
If you want to target more links, you'll need to update the '.msg, .entry'
selector to match the new elements too. I would suggest that you consider marking all of the elements you want to apply this to with a common class, like so:
<a class="entry statusLink" ..>...</a>
<a class="msg statusLink" ..>...</a>
<a class="another statusLink" ..>...</a>
Upvotes: 1
Reputation: 6547
Because you want the span to hide, you need to find the span inside the link. Like this:
$('.msg, .entry').click(function()
{
$('.number',this).hide();
});
Upvotes: 0
Reputation: 5143
A rough example of what you could do..
$(document).ready(function() {
$("a, .number").click(function() {
$(this).find(".number").hide();
});
});
Document ready checks that the DOM has been loaded before binding the functions to the events. $(this) means the object passed as a parameter, in this case the clicked object, whether that be .number or the link (a). The .find(number) is there to check that if the link has been clicked, it just hides the .number, not the whole link-tag.
Upvotes: 0