0

I'm trying to run a bit of a custom script to add a style class based on the length of the string in a h4 element. Basically, if the h4 is long enough that it takes up 2 lines, I'll want to adjust the CSS so that the h4 with a two line title also lines up with h4's that have a single line title.

A bit of JQuery returns the all the tags an their content..

jQuery(document).ready(function($){
  $('div.post-content div.title-info h4 a').text(function(){
      console.log(this);
  });
});

This returns..

<a href=​"http:​/​/​www.theaterunder30.com/​farewell-to-amazing-grace/​">​Farewell to Amazing Grace​</a>​
<a href=​"http:​/​/​www.theaterunder30.com/​details-announced-for-harry-potter-and-the-cursed-child/​">​Details Announced for “Harry Potter and the Cursed Child”​   </a>​
<a href=​"http:​/​/​www.theaterunder30.com/​another-blog-post/​">​Another Blog Post​</a>​
<a href=​"http:​/​/​www.theaterunder30.com/​arrested-development-quotes/​">​Arrested Development Quotes​</a>​

Which is great. But only part of the puzzle. I need to be able to count the characters only within the tags and then use something like .length to determine if it's greater than X amount of characters execute {...}

I'm not sure how I can access the text between the tags? in order to run a .length on it. I've had a look at this post but I honestly can't figure out how to apply it to my situation. Any help or guidance would be awesome.

Thanks for looking!

3
  • By text between tags, do you mean the element content? Commented Nov 19, 2015 at 19:19
  • try console logging $(this).text() Commented Nov 19, 2015 at 19:21
  • @DrunkWolf Yes, that would have helped. I was confused about how 'this' works and assumed that 'this' how I used it above should output the jQuery Selector with .text() applied. Commented Nov 19, 2015 at 20:53

3 Answers 3

6

You want to use jQuery .text() to get the text:

jQuery(document).ready(function($){
  $('div.post-content div.title-info h4 a').each(function(){
      console.log($(this).text().length);
      //Conditional logic based on text length here
  });
});
Sign up to request clarification or add additional context in comments.

4 Comments

This really isn't valid. You shouldn't be calling .text(function()).
This example is running the .text() function twice which has no sense.
I deleted my answer and vote for yours just because you edit it, but the real thing is that your answer was not right man ;)
I see where I've gone wrong! Thanks @DenimChicken, I thought I was calling the .text() function and so was confused when it output HTML as well, not understanding that .text() should have been applied to the console.log() and not the jQuery Selector.
0

I think you're looking for:

$(document).ready(function($){
                                         // V-- each not text!
  $('div.post-content div.title-info h4 a').each(function(){
    console.log($(this).text());
  });
});

Output:

​Farewell to Amazing Grace​

​Details Announced for “Harry Potter and the Cursed Child”​

​Another Blog Post​

​Arrested Development Quotes​

1 Comment

Phillips Thanks! Code is solid too!
-1

Check the length of the text, then if its longer than what you want (say 20 characters in this example) just add a css class.

$('a').each(function(){
    length = $(this).text().length;
    if(length > 20) {
        $(this).addClass('long');
    }
});

You can see it working on this JS Fiddle: https://jsfiddle.net/9s8evwcx/

8 Comments

This uses .text(function() in an invalid way.
Explain how? You can see its working on the JS Fiddle
Again using .text function twice, no sense.
your first method call to .text() makes no sense, it should be .each().
@AdamKonieska The text function just returns the text, the callback has no sense. Just: length = $('a').text().length
|

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.