1

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*');
4
  • Why have you closed the tag <a> prematurely? Commented Jul 15, 2015 at 10:59
  • My bad, sorry about that Commented Jul 15, 2015 at 11:02
  • are you looking for exact match or title containing the text categoryName? Commented Jul 15, 2015 at 11:05
  • 1
    How about $('.cat[title = "categoryName"]').append('*someText*'); Commented Jul 15, 2015 at 11:06

1 Answer 1

2

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>

Sign up to request clarification or add additional context in comments.

Comments

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.