views:

67

answers:

6

I have some text below called (16 Courses). I need to hide only this text, but I can't seem to hide it no matter what I try using jquery. Is there any help someone could provide so I can hide on this text?

<div id="programAttributes">
    <div class="left" id="credits">
       <h3>Credits</h3>
       <h3 class="cost">48</h3>
       (16 Courses)
    </div>
    <div class="gutter12 left">&nbsp;</div>
    <div class="left" id="costPer">
       <h3>Cost Per Credit</h3>     
       <h3 class="cost">$300</h3>                            
    </div>
</div>

I thought if I could write something like this that would do the trick, but I am so far unsuccessful.

$("#credits:not([class!=h3])").hide();
A: 

Could you wrap it in a separate span, and then do:

$('#credits span').hide();

?

a.feng
i've accepted the answer below, but unfortunately, i'm not able to write any new code within the body of the content. so adding span tags, for example, would not be possible otherwise i would write a new class for it and hide it that way. thank you feng for your support.
Evan
A: 

Try wrapping the text in a span as follows:

<div class="left" id="credits">
   <h3>Credits</h3>
   <h3 class="cost">48</h3>
   <span id="toHide">(16 Courses)</span>
</div>

then you can use jquery:

$("#credits > span)").hide();

the hide() function has to be applied to a DOM element.

Josiah
A: 

I would use a label tag around the text so I can handle it with jquery.

Lucia
Don't use a `<label>` tag unless it's actually a label for a form control. Just use a `<span>` with appropriate class and/or id set. (Assuming you are in control of the HTML source)
Stephen P
A: 

It's textnode. Loop thru all parents nodes and if it's type is textnode, hide it. See also this:

http://stackoverflow.com/questions/298750/how-do-i-select-text-nodes-with-jquery

yedpodtrzitko
+1  A: 

You can check for and remove textnodes like this:

​$("#credits").contents().filter(function() {
  if(this.nodeType == 3) 
    this.parentNode.removeChild(this);
});​​​​​​

You can test it here, this gets all the nodes (including text nodes) with .contents(), then we loop through, if it's a text node (.nodeType == 3) then we remove it.

Nick Craver
That would destroy the node completely. I think the OP would be much better off wrapping the text nodes in some other element, and hide and show on that.
Anurag
@Anurag - *If* the OP needs to show it again, yes...but that's an assumption at the moment. If that's the case you could easily wrap the node above in a `<span>`, etc...but I didn't making any more complicated than needed for the *effect* the OPs after at the moment.
Nick Craver
i just tested this and it works in production. i'm able to update the website template with the javascript to hide the course information.at ths time, we're unable to modify the code that writes the (16 courses) type information for about 3 weeks, because we have a build scheduled next week and the next production update won't be until next month. by that time though, we will have the fix in place where i no longer need to hide the text. so this was a temporary fix.
Evan
"This would still break if the node contained any other text"?
galambalazs
@galambalazs - This answer isn't *trying* to remove a single term, it's removing all text nodes, with a lot less processing.
Nick Craver
I see, just thought I give your comment back :)
galambalazs
+2  A: 

Usage

// hides in the whole document
hideText("(16 Courses)");

// only hide inside a specific element
hideText("(16 Courses)", $('#programAttributes'));

// make it visible again
showText("(16 Courses)"); 

[See it in action]

CSS

.hiddenText { display:none; }

Javascript

// escape by Colin Snover
RegExp.escape = function(text) {
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}

function hideText(term, base) {
  base = base || document.body;
  var re = new RegExp("(" + RegExp.escape(term) + ")", "gi");
  var replacement = "<span class='hiddenText'>" + term + "</span>";
  $("*", base).contents().each( function(i, el) {
    if (el.nodeType === 3) {
      var data = el.data || el.textContent || el.innerText;
      if (data = data.replace(re, replacement)) {
        var wrapper = $("<span>").html(data);
        $(el).before(wrapper.contents()).remove();
      }
    }
  });
}

function showText(term, base) {
  var text = document.createTextNode(term);
  $('span.hiddenText', base).each(function () {
    this.parentNode.replaceChild(text.cloneNode(false), this);
  });
}
galambalazs
This is not a good idea, though it's fancy in terms of show/hide, it also destroys *any* event handlers, among other things, on the entire document, for example: http://jsbin.com/uvuno3/3/edit Click any `<h3>` then run the functions, it'll break.
Nick Craver
thanks for pointing that out. I'll work on it.
galambalazs
**updated** now works with handlers.
galambalazs
+1 - although a little lengthy by jQuery's standards, it works.
Anurag
This would still break if the node contained any *other* text, needs a bit more refinement to be usable :)
Nick Craver
yes I've implemented a new version.
galambalazs