/    Sign up×
Community /Pin to ProfileBookmark

Onclick inside innerHTML

hello,
what is wrong whis this code?

<div onclick=”test(44)”>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)
3-

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@WyCnetMar 05.2013 — <?php

$str3=<<< ENDd

<html>

<head>

<script type="text/javascript">

function test(x){alert(x)};

</script>

<body>

<div id="MYdiv"> this id My div</div>

<div onclick="test(44)">anything</div>

<script type="text/javascript">

var col=document.getElementById('Mydiv');

col.innerHTML='<div onclick="alert(2);test(123);">Text</div>';

</script>

</body>

</html>

ENDd;

echo $str3 ;
[/QUOTE]


The above version works.
Copy linkTweet thisAlerts:
@group99_001authorMar 05.2013 — The above version works.[/QUOTE]

No , it didnt work with me ,

it fired test(44) , but what i need is to fire test(123) that's inside innerHTML
Copy linkTweet thisAlerts:
@WyCnetMar 05.2013 — If you notice, you have shown nothing with id "Mydiv".
<div onclick="test(44)">anything</div>

< script>

function test(x){alert(x)};

var col=document.getElementById('Mydiv');

col.innerHTML='<div onclick="alert(2);test(123);">Text</div>';

< /script>[/QUOTE]


therefore : add the id = "Mydiv", as shown below:

<html>

<head>

<script type="text/javascript">

function test(x){alert(x)};

</script>

<body>

<div id="MYdiv" onclick="test(44)">anything</div>

<script type="text/javascript">

var col=document.getElementById('Mydiv');

col.innerHTML='<div onclick="alert(2);test(123);">Text</div>';

</script>

</body>

</html>
[/QUOTE]
×

Success!

Help @group99_001 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...