Home › Forums › JavaScript › [Solved] jQuery Removing One Word of Text
- This topic is empty.
-
AuthorPosts
-
September 8, 2013 at 3:25 pm #149442LegacyP7Participant
Hello. I’m trying to remove just one word of text from a div.
Here’s the code:
<div>
<a href="#">username here</a> posted <span>Aug 23, 13</span>
</div>
I want to remove the word “Posted” – as I have applied CSS to the rest, and it is standing out like a sore thumb! (Image: http://puu.sh/4mzfx.png)
Now; I’ve tried this bit of jQuery code – which was what I made up from what I found on Google:
$(".info:contains( posted )").text("");
However, if I use this – everything is removed. The Date, the username and everything. This jQuery is selecting the whole .info class – rather than just the word. How can I select just that word?
Thank you!
September 8, 2013 at 4:54 pm #149445SenffParticipantI would actually dig in the code to see if I could remove the word from the source code, but if you don’t have access to that, you could do it with just CSS.
Give the whole line a font size of 0, then give tha A and the SPAN a font size of 14px (or anything) so that the zero font size is only applied to the word “posted”. Something like this: http://codepen.io/senff/pen/oCGaq
September 9, 2013 at 3:59 am #149480Paulie_DMemberIf I may. the word posted should be wrapped in a text tag such as a
<p>
or<span>
…at the moment it’s just sitting there as (I think) a text node in the HTML.Because it doesn’t have a text tag it can’t be selected directly with CSS.
Wouldn’t that be better…and more semantic(?) or at least proper HTML anyway?
<div> <a href="#">username here</a> <p class="posted">posted<p> <span>Aug 23, 13</span> </div> p.posted { display:none; }
September 9, 2013 at 8:50 am #149513TheDocMember@Paulie_D – I don’t think that’s semantic at all since it’s a single sentence.
Really, it’d be
<p><a href="#">Username</a> posted on <span class="date">Date</a></p>
.This is certainly not pretty, but if it has to be done via JS you could do this with jQuery:
http://codepen.io/ggilmore/pen/103c948cf7d841dc5eada185a94fd1d7
September 9, 2013 at 8:58 am #149516Paulie_DMember@The Doc.
You’re probably right…swap out the
p
for aspan
and it’s easier/better.<div> <a href="#">username here</a> <span class="posted">posted</span> <span>Aug 23, 13</span> </div>
The important point to me is that all text should be inside some sort of text(like) tag such a p, span, heading or a etc.
September 9, 2013 at 1:59 pm #149538LegacyP7ParticipantThis is the exact code. I’ve removed the link though.
<div class="info">
<a href="#" class="#">Joe_Nub</a> posted <span class="date">Aug 23, 13</span>
</div>I need to just be able to select the word – as it has no tagging, and I’m using Enjin, which is kinda like WordPress, and I cannot directly change up the code.
As forth, your responses wouldn’t work.
September 9, 2013 at 2:43 pm #149545TheDocMemberAs forth, your responses wouldn’t work.
My response would definitely work.
September 9, 2013 at 4:50 pm #149554SenffParticipantSo what am I, chopped liver?
With just CSS:
.info { font-size:0; } .info a, .info span { font-size:14px; margin-right:5px; }
Or, if you prefer, with just Javascript/jQuery (using
replace()
): http://codepen.io/senff/pen/wGuIBSeptember 10, 2013 at 10:46 am #149661TheDocMember@Senff ;)
Actually, when I viewed your solution in Codepen the result was completely blank so I thought it wasn’t working. Looking at it now it appears to be working beautifully.
September 10, 2013 at 2:01 pm #149668LegacyP7ParticipantOh goodness, how did I not think of the CSS method! Thank you!
I was trying to overcomplicate things with jQuery!
September 11, 2013 at 11:00 am #149736 -
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.