calledmaxi
calledmaxi

Reputation: 83

get iframe by its title with javascript

I want to get the iframe by it's title. The iframe doesn't have a name, id or a class. I want to get the Iframe just by it's title with plain javascript.

This is the iframe :

<iframe title="iframetitle" frameborder="0" scrolling="no"></iframe>

Please don't use Jquery or any plugin!

Upvotes: 1

Views: 1157

Answers (1)

Get Off My Lawn
Get Off My Lawn

Reputation: 36311

Use a querySelector() to find the iframe. A value inside of brackets searches for attributes [name], [title], [data-descr], etc. When adding an = something, you then are also searching the value, so [title=iframetitle] would look for something that has a title attribute with a value of iframetitle.

We could enhance the query more by saying it also has to be an iframe by doing iframe[title=iframetitle].

Here is a working example:

console.log(document.querySelector('iframe[title=iframetitle]'))
<iframe title="iframetitle" frameborder="0" scrolling="no"></iframe>

Upvotes: 3

Related Questions