user3275707
user3275707

Reputation: 89

Target an html element based on title tag using JQuery

How to target an HTML element based on its title tag?

let's say we have:

<a class="cat" title="categoryName">categoryName</a>

How to target this anchor element using its title tag then append some text to it.

I tried this but it doesn't work:

$(".cat['title' = *categoryName*]").append('*someText*');

also tried this but it doesn't work:

$(".cat['title' ~= *categoryName*]").append('*someText*');

Upvotes: 1

Views: 377

Answers (1)

ᔕᖺᘎᕊ
ᔕᖺᘎᕊ

Reputation: 3011

21 problems:

  • You've got an invalid a tag - you've prematurely closed it, and
  • your selector is wrong. See the Attribute Equals Selector section at the jQuery docs.

This works:

$(".cat[title = 'categoryName']").append('someText');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="cat" title="categoryName">categoryName</a>

Upvotes: 2

Related Questions