0

Here is my code:

<div id="authorarea">
<img alt="" src="#" height="70" width="70">
<p class="written">
    Written by: <a href="http://www.revitalagency.com/author/admin/" title="Steven Stamkos" rel="author">Steven Stamkos</a>
</p>
<div class="authorinfo">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

<div id="authorarea">
<img alt="" src="#" height="70" width="70">
<p class="written">
    Written by: <a href="http://www.revitalagency.com/author/admin/" title="Nikita Kucherov" rel="author">Nikita Kucherov</a>
</p>
<div class="authorinfo">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

The only differences are the "title" and the content inside of the a tag.

I tried to do something like this but I am not grabbing the title correctly:

if ($('#authorarea .written a').attr('title', 'Steven Stamkos')) {

$('#authorarea .authorinfo').append('<div>test</div>');

} else if ($('#authorarea .written a').attr('title', 'Nikita Kucherov')) {

$('#authorarea .authorinfo').append('<div>test2</div>');

}

So I'm looking for suggestions on how to target the title correctly or the content inside of the a tag. Any help is much appreciated and I hope I explained what I'm trying to do properly enough.

user3330820
  • 501
  • 2
  • 7
  • 17

1 Answers1

2

Your current code

if ( $('#authorarea .written a').attr('title', 'Steven Stamkos') ) {}

sets the attribute to the given value and then returns a jQuery object which naturally evaluates to true.

What you probably want is

if ( $('#authorarea .written a').attr('title').localeCompare('Steven Stamkos') === 0 ) {}
Etheryte
  • 24,589
  • 11
  • 71
  • 116
  • 1
    Could you tell us please why you use `.localeCompare` instead of the standard `==`? – Al.G. Jul 09 '15 at 19:13
  • @Al.G. Using `==` is the exact opposite of standard, see [this SO question](http://stackoverflow.com/questions/2167602/optimum-way-to-compare-strings-in-javascript), for example, for further discussion. – Etheryte Jul 09 '15 at 19:15
  • @Nit is it more efficient than `===` ? – Mindastic Jul 09 '15 at 19:19
  • @Mindastic exactly what I was about to ask... couldn't find any sources against `==`... – Al.G. Jul 09 '15 at 19:23
  • @Al.G. but `==` is not the same than `===` . `==` uses implicit conversion of data types and is not performant. – Mindastic Jul 09 '15 at 19:24
  • Yeah, I understand, same is in php, but why to use `localeCompare` and not `===`? Later is shorter and cleaner? – Al.G. Jul 09 '15 at 19:26
  • The main benefit of `localeCompare` is that you can use locales, as the name implies, that is, you can account for location-specific differences in the alphabet easily. – Etheryte Jul 09 '15 at 19:56