Jump to content

Targeting A Text Node


jimfog

Recommended Posts

I want to target a specific text node and wrap it in a p tag. The text node is contained in an element with class name calendar. This text node is just the output of getMonth method.(November), here is the code i used:

$('.calendar').contents().filter(function() {  return this.nodeType == 3;}).wrap('<p class="month"/>')

The problem is that 2 text nodes get the class month, the one has in it the month and the other is empty:

<p class="month">November</p><p class="month"></p>

How i must code that the outcome is only one p class, with the month in it-i do not want a second p class. Obviously, in that place the DOM has 2 text nodes(i do not now why this happens).As such, It is logical to have such an outcome. So how i must do it.

Link to comment
Share on other sites

On second thought, another approach is to target all the empty p nodes for removal-<p class="month"></p>, the code to remove the preceding is this:

$("p:empty").remove();

The above does not work though, and i cannot figure why.

Link to comment
Share on other sites

Here are 2 code segments that might help, the first one is from Firebug DOM tab and the second from the html:

textContent"\n \n \n"

The above is what is contained in the <p class="month"></p> elementaccording to the DOM tab in firebug.

<div class="calendar"><p class="month">November</p><p class="month"></p><table border="1" width="200">    <tbody><tr></table><p class="month"></p></div>

As you see, above(HTML) there are 2 p empty nodes having class month, i just want these removed.

Link to comment
Share on other sites

Unfortunately, I still don't have enough information. I could make some guesses, though. You could try to make sure that a node has more than just whitespace like this:

return  (this.nodeType == 3 && /[^\s\t\r\n]/.match(this.nodeValue));

If it's possible, upload a working example of the problem.

Link to comment
Share on other sites

Ok i am going to add some more information so we can get at the bottom of this, hopefully:

$('.calendar').contents().filter(function() {  return this.nodeType == 3;}).wrap('<p class="month"/>');

The above code gets the text nodes inside .calendar and wraps them in p tags. And the code that follows is what it was before the above operation:

<div class="calendar">November<table width="200" border="1"></div>

The "November" you see is output from a getMonth method. I want to wrap in p tag the month. Now i am thinking it over again.Maybe when using the .contents method of jQyery i should aim solely for the month and not the text nodes in general and i would need some help about how to code that. What do you think?

Link to comment
Share on other sites

Here is what i tried and worked:

$("p").remove(":contains('\n')");

I am not over though, there are still p tags that just have white space in them and newlines as is thecase above.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...