0

So, I am trying to get a data attribute from an element that is inside an iframe. This iframe is inside our website. So, when I click on the next button I have a callback that has the following code.

onNext : function(){
   var $img = $(".tacobowl").contents().find(".url").data('img');
   var $imgFolder = $(".tacobowl").contents().find(".url").data('imgFolder');
   alert($img + ' ' + $imgFolder);    
},

This code returns an alert box that says "undefined undefined".

The class taco is attached to the <iframe class="taco"></iframe>

Then this is the html inside the iframe.

<div class="url" data-img="small" data-imgFolder="breakfast">

</div>

I have been able to add classes by using the following code. But, I have not been able to get the data attribute from this same div.

  $(".tacobowl").contents().find(".url").addClass("taco100");

Is it even possible to get the data attribute in such a way?

Maistrenko Vitalii
  • 994
  • 1
  • 8
  • 16
Divern
  • 343
  • 2
  • 15
  • How to select the data attributes is well answered at https://stackoverflow.com/questions/5309926/how-to-get-the-data-id-attribute If you will provide more details of the abstract html structure in your page, you will get solution quicker. It is unclear here what is `taco` class bind with. – Kamal Singh Oct 06 '17 at 03:33
  • Alright if `taco` class is for `iframe` as you have mentioned in below answer comment, selecting element inside iframe is answered on https://stackoverflow.com/questions/15343955/jquery-select-element-inside-iframe-which-is-inside-an-iframe – Kamal Singh Oct 06 '17 at 03:40

1 Answers1

0

Try:

$('.url',$(".taco").contents()).data('img');

$(".taco") is your iframe, right?

Taufik Nur Rahmanda
  • 1,862
  • 2
  • 20
  • 36
  • is element with class name `url` exist inside your iframe? or even there's more than one? maybe, you must have more specific selector than `'.url'` – Taufik Nur Rahmanda Oct 06 '17 at 03:45
  • Yes, the page that the iframe loads has a div with the class "url". That is how I am able to add a class to it if it loaded via iframe. I will try another solution. I might delete this if I find it. – Divern Oct 06 '17 at 03:50
  • @Divern did you went through the pages link I mentioned on the question comment? Things will get clear if you read those nice posts. – Kamal Singh Oct 06 '17 at 03:53
  • @Divern I see, but in your later do, you use `.tacoBowl` iframe, not `.taco`, that's why I asked to confirm it. Now there's 2 possibility for the problem, but before, please try `console.log($('.url',$(".taco").contents()).length);`. What appear? – Taufik Nur Rahmanda Oct 06 '17 at 03:57
  • This was a mistake on mine @TaufikNurRahmanda. Thanks for pointing it out. It worked for me. Thanks! – Divern Oct 06 '17 at 04:00