473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Get the name/id of an iframe onClick

Hi,

I have an interesting problem that i need to solve, i have been developing a simple rich text editor which can go multiple times on the same page. What i need to do is make it so that when you click anywhere on the IFRAME it will return the id of the iframe. I have the first part done, so i can click on the iframe and call a function but how would i go about using a function to get the id of the IFRAME.

Thanks for your assistance,
Steve
Oct 1 '07 #1
9 2943
gits
5,390 Expert Mod 4TB
hi ...

please post the (simplified) code that relates to your problem ... how did you try to retrieve the id?

kind regards
Oct 1 '07 #2
To go into abit more detail i am assigning an event handler like so

Expand|Select|Wrap|Line Numbers
  1. document.getElementById(i).contentWindow.document.addEventListener("click", function() { alert(i) }, true);
  2.  
This event handler is assigned within a loop, with i being the iterating number.

But when this event handler is actually called when someone clicks on the iframe, i will always contain the last item in the loop. I have 3 editors and i want the function to return what i is equal to at that point in the loop, but instead it will always return 3, instead of 1 or 2.

I cant use things like this, because that is only relevant to items within the iframe, not the iframe itself.


I hope that makes abit more sense.

Thanks
Steve
Oct 1 '07 #3
gits
5,390 Expert Mod 4TB
hi ...

try using:

Expand|Select|Wrap|Line Numbers
  1. node.addEventListener('click', function(e) { alert(e.target.id) }, true);
  2.  
note: this works with mozilla, ff, safari, opera - ie uses e.srcElement for target ... so you have to adapt it for that ...

kind regards
Oct 1 '07 #4
Hi,

I tried this out and it appears to return the parent node within the editable iframe content, instead of the iframe itself.

Any other suggestions?

Thanks,
Steve
Oct 1 '07 #5
gits
5,390 Expert Mod 4TB
hmmm ...

as far as i can see you do the following another way:

Expand|Select|Wrap|Line Numbers
  1. var node = document.getElementById('test_id1');
  2.  
  3. node.contentDocument.addEventListener('click', function() { alert(i) }, true);
  4.  
so the handler is assigned to the contentDocument ... i think your code is equivalent to this shorter one here. try:

Expand|Select|Wrap|Line Numbers
  1. node.contentDocument.addEventListener('click', function() { alert(node.id) }, true);
kind regards
Oct 1 '07 #6
Hi,

I just gave this a shot, but unfortunately it yields the same results as just doing alert(i).

The last item in the loop is used, in this case 3. So regardless of which textarea i click on the result returned is always 3.

Thanks,
Steve
Oct 1 '07 #7
gits
5,390 Expert Mod 4TB
hi ...

its a little bit tricky with that anonymous functions ... play with the following working example, that stores the variable in each of the applied handler functions:

[HTML]
<html>
<head>
<script type="text/javascript">

function add_events() {
var click_handler = function(node) {
return function() { alert(node.id) };
};

for (var i = 1; i < 4; i++) {
var node = document.getElementById(i);

node.addEventListener('click', click_handler(node), true);
}
}

</script>
</head>

<body onload="add_events();">
<p id="1" name="test_name">test</p>
<p id="2" name="test_name">test</p>
<p id="3" name="test_name">test</p>
</body>

</html>
[/HTML]
you may adapt it to your needs ...

kind regards
Oct 1 '07 #8
thank you for your help, saved me days figuring it out, all works perfectly now :)
Oct 1 '07 #9
gits
5,390 Expert Mod 4TB
ha ;) ... it works ;)) ... glad that it would be of help to you ... post back to the forum anytime you have more questions ...

kind regards
Oct 1 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Wendi | last post by:
If i have an iframe from within an HMTL document: Iframe.htm: ------------------------------ <html> <head> <title>Iframe.htm</title> </head> <body> <iframe src="sample.htm"></iframe>
5
by: Mel | last post by:
i need to have 2 side by side iframes, a link on top of the one will show/hide the other can someone help me pleeeeezzzzz ?
5
by: chris.poirier | last post by:
Does anyone know how I could make an iframe so that the links in the frame page are disabled ... or if this is possible? Any help is appreciated.
1
by: Testing12340 | last post by:
Hello!, I have this well, little IE issue. I'm showing hiding div's (containing IFRAMES), however, for some reason in IE one of the iframe contents allow me to click them (the last one allows me...
4
by: Venkatesh | last post by:
Hello All, I have an iframe in my main html and within iframe, I'm loading another HTML webpage. In my main html page, I've captured the mouse click event, by setting the "onclick" for <bodyof...
1
by: Andrew Poulos | last post by:
If I do this: el = document.createElement("iframe"); el.src = "test.htm"; el.name = "frm"; // or el.setAttribute("name","frm"); document.body.appendChild(el); the iframe gets created but the...
3
by: Asterbing | last post by:
I want to know the length in pixels of a document embedded in a iframe. The way I go seems to work under IE but not under Firefox (about other browsers I'll see later). Here is my page for...
2
by: ericisjusteric | last post by:
I have a page with multiple iframes and need to have the user (ie6) be able to click a button to refresh any one of the iframes - but also to click another button at the top of the page to refresh...
3
by: Archana | last post by:
hi all, can any one tell me how will i set scrollbar to iframe throguh javascript. please help me asap. thanks in advance
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.