Click here to Skip to main content
15,908,455 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi i have div that reapited ! all div are one ! Example :
HTML
<div class="ow_chat_dialog" id="main_tab_contact_11">
<iframe id="myframe"></iframe>
<button onclick="setsrc()"></button>
</div>

<div class="ow_chat_dialog" id="main_tab_contact_12">
<iframe id="myframe"></iframe>
<button onclick="setsrc()"></button>
</div>

<div class="ow_chat_dialog" id="main_tab_contact_13">
<iframe id="myframe"></iframe>
<button onclick="setsrc()"></button>
</div>
14
15
16
.
.
....
<script>
function setscr(){
document.findviewbyid("myframe").src="hrl..."
}
</script>


But wen i click on button change the first iframe element !!!
plz help me i need help :((
Posted

First things first, you HTML is illegal. Because there is only one ID allowed with a specified identifier string. Yours has 3 of them. Change the HTML to this one,

HTML
<div class="ow_chat_dialog" id="main_tab_contact_11">
   <iframe class="myframe"></iframe>
   <button onclick="setsrc()"></button>
</div>
 
<div class="ow_chat_dialog" id="main_tab_contact_12">
   <iframe class="myframe"></iframe>
   <button onclick="setsrc()"></button>
</div>
 
<div class="ow_chat_dialog" id="main_tab_contact_13">
   <iframe class="myframe"></iframe>
   <button onclick="setsrc()"></button>
</div>


This is why you use class to group elements of same type. The problem that it selected the first, was because it started looking for any element with an ID of myframe, and the first one it found was applied with the source of resource.

Secondly, to get the nearest, or the iframe inside the same parent you can use any of the following methods, do use jQuery to make things a little simple.

Finding from the same parent

JavaScript
$(document).ready(function () {
   $('button').click(function () {
      // remove the event hanlder from the HTML markup
      // handle it here..
      $(this).parent().find('iframe').attr('src', "http://www.locationofresource.com/page.html";
   });
});


This code, would find the iframe inside the same parent where the button is present and will update the source of it.
 
Share this answer
 
Comments
‫محم د‬‎ 6-Dec-14 5:12am    
i shuld use javaScript ! Not jquery
Afzaal Ahmad Zeeshan 6-Dec-14 5:25am    
Then searching for a closest would be a tough time for you. Have a look at this question and see if you can get the clue of finding the closest element. http://stackoverflow.com/questions/18663941/finding-closest-element-without-jquery
hi,

As i understand

try this

To get a div by class name:

get Div by Class name

var allDivs = document.getElementsByClassName("ow_chat_dialog");
Assuming there's only one

var calendarDiv = document.getElementsByClassName("ow_chat_dialog")[0];
Get the iFrame inside:

var iFrame = calendarDiv.getElementsByTagName("myframe")[0];

Use Like this in your fn

function setscr(){
var calendarDiv = document.getElementsByClassName("ow_chat_dialog")[0];
Get the iFrame inside:

var iFrame = calendarDiv.getElementsByTagName("myframe")[0];
document.findviewbyid("iFrame ").src="hrl..."
iFrame ..src="hrl..."
}
 
Share this answer
 
Comments
‫محم د‬‎ 6-Dec-14 5:37am    
i write Ur code But it give an Error ! :
Uncaught TypeError: undefined is not a function
why ? thank for help me ...
mudgilsks 6-Dec-14 6:06am    
modify this code according to your reqirment
va Input = $(this).closest('.row').find('.inputQty');

$(document).ready(function(){
$("span").closest("ul").css({"color":"red","border":"2px solid red"});
});
 
Share this answer
 
Comments
‫محم د‬‎ 6-Dec-14 6:14am    
i neeeeeed javaScript Not jQuery !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900