b sovs
b sovs

Reputation: 31

Is there a way of selecting elements by their title in JS?

I am trying to create a program to select elements on the page, but it seems that the only thing that makes them unique are there titles. Is there any way of selecting certain things on a web page by their titles? I found this script but it will not select or click on the elements.

What am I doing wrong? Any help will do. Thanks

How can I get it to work on A and not div, sorry, that was the problem My script that I am using --

     $(document).ready(function () {
         $("a[title=\"Learn More About Becoming A VIP\"]").click();
     });

It says Div is not defined when I try to run script. Why So?

Upvotes: 0

Views: 3535

Answers (2)

Rounin
Rounin

Reputation: 29463

There is a way to select elements by their title attribute in javascript:

document.querySelectorAll('div[title="john"]');

======

You can also select elements that have a title beginning with john:

document.querySelectorAll('div[title^="john"]');

Or elements that have the (space-bounded) word john somewhere in their title:

document.querySelectorAll('div[title~="john"]');

Upvotes: 2

leo.fcx
leo.fcx

Reputation: 6467

Your selector should work if title value is john. You might wan't to select element that contains certain string as follow:

$('div[title*="john"]')

Upvotes: 1

Related Questions