3

In the following html, can I hide the content starting from ( and ending at ) using CSS or jQuery. The problem I'm facing is that the ( and later code is not wrapped in any div, so I can not use CSS to find the div class and set display:none.

<span>
    <a class="test" href="#">test</a> 
    (<a href="#">?</a>) 
</span>
7
  • So assign an ID or a class to the second <a href> and hide that. Or is that not an option? Commented Jul 26, 2012 at 19:20
  • So: what is special with this a tag? If the answer is nothing, then it'll be hard to target... Commented Jul 26, 2012 at 19:23
  • you want to leave or hide the parenthesis? Commented Jul 26, 2012 at 19:23
  • If I'm understanding correctly, you want to hide the second a tag as well as the parenthesis on either side of it, but not the first link, right? Commented Jul 26, 2012 at 19:23
  • @ZoltanToth want to hide the parentheses and the content inside it. Commented Jul 26, 2012 at 19:24

6 Answers 6

3

$('span a:not(".test")').hide(); will work on your limited example.

However, it won't hide the parentheses. To do that, you'll need a specialized function to remove just the code you don't want:

$('span').html(function(i,old) {
    return old.substring(0,old.indexOf('('));
});​

http://jsfiddle.net/pkMgP/

If you want to hide that instead of removing it, you'll need to wrap it all up in an HTML element and apply styles to that:

$('span').html(function(i,old) {
    var idx = old.indexOf('(');
    return old.substring(0,idx) + '<span style="display:none">' + old.substring(idx) + '</span>';
});​

http://jsfiddle.net/pkMgP/1/

Sign up to request clarification or add additional context in comments.

Comments

0

have you tried using the element display:none; ?

4 Comments

i mean as in an inline style style="display:none;"
this should really be a comment to the op and not an answer.
I would if I knew where the "add comment" is
same way you added these comments :-) just at the bottom of the op.
0

You can't hide parts of DOM nodes, only complete nodes.

You'll need to change your markup to wrap the entire link including the parentheses in a single <span>.

This could be done in Javascript, but it would be far easier to do it at source.

Comments

0

Using jquery:

$('.test').next().hide();​

This will leave just the parenthesis

Comments

0

I had to reread you post a few times, but I think I see what you want. You won't be able to do what you're asking directly.

If you can edit the HTML then add a inner span surrounding the content you want to hide and hide it.

I'm thinking you're asking this question because you can't edit the HTML. In that case, what you could do is:

  1. construct a JQuery expression to find the outer span element
  2. Hide the span element using $(e).css('display','none')
  3. Use javascript to copy the contents of the span, remove the stuff you want to hide
  4. Create a new span containing the contents created in #3, add it to the DOM right before or after the span you've hidden.
  5. You can now toggle back and forth to make the contents swap visibility.

2 Comments

You can also use something like what Blazemonger suggests, if you do you can stash the hidden content away in the .data() for the jquery node, or keep it in an array somewhere.
yes I want to hide without changing HTML, and yes, I'll look at the answer by Johndave and Blazemonger.
0

Simply add some classes to define actions.

<span>
  <a class="go" href="#">test</a> 
  (<a href="#" class="hideme">?</a>) 
</span>

$('.go').bind('click', function(e){
    $('.hideme').hide();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.