0

I have a number buttons which when pressed open a modal with an iframe. The buttons have data attributes that store the link which I want to pass to the iframe scr.

This is the code for the buttons:

<a href="#myModal" style="float: right;" class="btn btn-outline-secondary" data-toggle="modal" data-jobid="index.php?/job/view/1" ><strong>View</strong></a>

<a href="#myModal" style="float: right;" class="btn btn-outline-secondary" data-toggle="modal" data-jobid="index.php?/job/view/2" ><strong>View</strong></a>

<a href="#myModal" style="float: right;" class="btn btn-outline-secondary" data-toggle="modal" data-jobid="index.php?/job/view/3" ><strong>View</strong></a>

This is the code for the iframe:

<iframe style="display: block; width: 100%; height: 100%; border: none;" src=""></iframe>

My question is how can I pass the data-jobid to the src of the iframe each time one of the buttons is clicked?

1
  • 1
    I wonder why someone downvoted this? I don't see anything wrong with how the question was phrased? Commented Jan 31, 2018 at 15:09

2 Answers 2

3

You can hook a click handler to the .btn-outline-secondary elements, then read the data() attribute from them before setting the src attribute of the iframe, like this:

$('.btn-outline-secondary').click(function() {
  $('iframe').attr('src', $(this).data('jobid'));
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Rory, I learned something new today. I never realized $(this).data('jobid') is the same as $(this).attr('data-jobid').
Glad it helped you :) Also, just FYI, they're not quite the same. attr() reads the attribute from the DOM, whereas data() reads it from jQuery's internal cache. So long as you use the same method to get/set the value it's fine. Personally I prefer data() as it's faster in most situations.
0

You can do it via jQuery like this:

$(".btn")click(function(){
var data = $(this).attr('data-jobid');
$(iframe).attr('src', data);
});

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.