1

Hi I want to access all function just do in a div ! for Example :

<div class="ow_chat_dialog" id="main_tab_contact_11">
<iframe id="myframe"></iframe>
<button onclick="sersrc()"></button>
</div>

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

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

in this code i can create some div with one class name and id... But when i click on any button the first iframe change ! if i want just change the sibling iframe ! ** just use javaScript , Not Jquery .

2
  • invalid html, duplicate id myframe Commented Dec 6, 2014 at 8:27
  • So , how i can solve it ? this div create by user ... Commented Dec 6, 2014 at 10:03

2 Answers 2

2

Don't use ids, they must be unique. Use classes instead:

<div class="ow_chat_dialog" class="main_tab_contact_13">
    <iframe class="myframe"></iframe>
    <button onclick="sersrc(this)"></button>
</div>

Then let sersrc know which button was clicked by passing button reference. From there you can find sibling iframe like this for example:

function setscr(obj) {
    obj.parentNode.querySelector(".myframe").src = "hrl..."
}

If you are sure that iframe is going to be always immediately before the button then you can simply do

obj.previousElementSibling.src = "hrl..."

(course this is less reliable since depends on structure).

Sign up to request clarification or add additional context in comments.

7 Comments

Uncaught TypeError: Cannot read property 'querySelector' of undefined
Did you pass this reference: onclick="sersrc(this)" ?
i use this : var frame = this.parentNode.querySelector(".myframe"); frame.contentWindow.location.href = 'localhost:8585/demos/project/index.html';;
You should use obj.parentNode, because this is not button, but global object window.
agane Error : Uncaught TypeError: Cannot set property 'src' of null
|
0

var els = document.querySelectorAll('.ow_chat_dialog > button');
function handler () {
  this.previousElementSibling.src = "//stackoverflow.com";
}
for (var i=0; i<els.length; ++i)
  els[i].addEventListener('click', handler);
<div class="ow_chat_dialog">
  <iframe id="myframe"></iframe>
  <button></button>
</div>
<div class="ow_chat_dialog">
  <iframe id="myframe"></iframe>
  <button></button>
</div>
<div class="ow_chat_dialog">
  <iframe id="myframe"></iframe>
  <button></button>
</div>

6 Comments

how i can use ur code in a function ? if it is Further what ? dose we have previousElementSibling in javaScript ?
if i chage iframe and button ! first button and then iframe can i use ur code ? i want use ur javaScript code in button onclick !
Avoid inline event listeners. Just write the code you want to run at on click in handler function.
Uncaught TypeError: Cannot set property 'src' of undefined
You are doing something wrong. this.previousElementSibling can't never be undefined.
|

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.