Max
Max

Reputation: 25

Change title iframe (that has no id) within a div

I'm trying to figure out how to add a title to a iframe that has no 'id' using javascript.

The code is like this:

<div class="review-right">
<div>
<iframe></iframe>
</div>
</div>

And I want to get this:

<div class="review-right">
<div>
<iframe title="Sample"></iframe>
</div>
</div>

This is what I've tried:

<script type="text/javascript">
$(document).ready(function(){
   $(".review-right iframe").attr('title', 'Sample');
});
</script>

I'm a noob at jquery so I don't know if this is even right.

Upvotes: 1

Views: 498

Answers (1)

SomeRandomOwl
SomeRandomOwl

Reputation: 33

Try removing the . from .review-right class in the HTML and in the Script just use basic DOM

document.querySelector(".review-right div iframe").title = 'Sample Vanilla'
<div class="review-right">
  <div>
    <iframe></iframe>
  </div>
</div>

JQuery Solution

$(document).ready(function(){
   $('.review-right iframe').prop('title', 'Sample Jquery');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="review-right">
  <div>
    <iframe></iframe>
  </div>
</div>

Upvotes: 1

Related Questions