Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-05-2013, 11:41 AM   PM User | #1
group99_001
New to the CF scene

 
Join Date: Mar 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
group99_001 is an unknown quantity at this point
Exclamation Onclick inside innerHTML

hello,
what is wrong whis this code?

<div id="Mydiv">anything</div>
<script>
function test(x){alert(x)};
var col=document.getElementById('Mydiv');
col.innerHTML='<div onclick="alert(2);test(123);">Text</div>';
</script>


notes:
1-alert(2); works , but test(123) not working , test(44) is working(outside innerHTML)
2- i want to use parameters inside test() functions i.e: test(123)
group99_001 is offline   Reply With Quote
Old 03-05-2013, 01:08 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
This is working fine in all tested browsers (Firefox, Chrome): http://jsbin.com/awizux/1/edit

So the problem must be somewhere else.
devnull69 is offline   Reply With Quote
Old 03-05-2013, 06:23 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,454
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
You can't rely on JavaScript added inside of innerHTML always running correctly across all browsers - Internet Explorer in particular has many restrictions on how and where innerHTML can be used. If you want the HTML to be able to be properly accessed from JavaScript you should add it using DOM mcalls instead of innerHTML.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 03-05-2013, 11:40 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Specifically, something like:
Code:
<div id="Mydiv">anything</div>
<script>
function test(x){alert(x)};

var newdiv = document.createElement("div");
newdiv.innerHTML = "Text";
newdiv.onclick = function() { test(123); }

mydiv = document.getElementById('Mydiv');
mydiv.innerHTML = ""; // optional!
mydiv.appendChild( newdiv );
</script>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:39 AM.


Advertisement
Log in to turn off these ads.