user1547554
user1547554

Reputation: 591

Adding title to dynamically generated iframe

I'm generating the iframe dynamically and adding a title for that but instead of the given title I'm getting the Action Method name as the title. My code is:

("#modalBody").empty().append('<iframe title="This is the title" id="iframeID"></iframe>');
$("#iframeID").attr("src", "../Employee/GetEmployeeDetails?empID=" + empID);
$('#empModal').modal('show');

Title is coming as: GetEmployeeDetails instead of 'This is the title'.

Any help would be appreciated. Thanks.

Edit: I found one similar question but here the iframe has been generated dynamically while in that question iframe was not generated dynamically.

Edit -2 This is also giving the same result:

 ("#modalBody").empty().append('<iframe title="This is the title" id="iframeID"></iframe>');
    $("#modalBody iframe").contentDocument.title = 'My New title!';
 $("#iframeID").attr("src", "../Employee/GetEmployeeDetails?empID=" + empID); $('#empModal').modal('show');

Upvotes: 0

Views: 340

Answers (1)

CR41G14
CR41G14

Reputation: 5594

I cannot seem to replicate what you have posted but the below markup is working for me:

HTML

<div id="modalBody">
  <div id="somemarkuphere">

  </div>
</div>

JS

$(document).ready(function() {
  $("#modalBody").html('<iframe title="This is the title" id="iframeID"></iframe>');
   $("#modalBody iframe").attr("src", "../Employee/GetEmployeeDetails?empID=" + 1);
  $("#modalBody iframe").attr("title", 'My New title!');
});

https://jsfiddle.net/o2gxgz9r/12781/

Upvotes: 1

Related Questions