473,651 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

innerHTML and javascript

sabesneit
4 New Member
Hi, I'm tring to execute some javascript with innerHTML but nothing happen, here a simplified example:
[HTML]
<html>
<head>
<title>this doesnt work!</title>
<script language="javas cript">
function DoSomething()
{
document.getEle mentById("div1" ).innerHTML = "<script language=\"java script\">\n alert(\"hello world\");\n <\/script>";
}
</script>
</head>
<body>
<div id="div1"></div>
<input type="button" onClick="DoSome thing();" value="damn do something!">
</body>
</html>
[/HTML]

if you find out why it doesnt pop "hello world" or the way to do it, thank to let me know.
Sébastien
Nov 15 '06 #1
3 3156
ronverdonk
4,258 Recognized Expert Specialist
Because the alert is not triggered. The following does work:
[html]
<html>
<head>
<title>this doesnt work!</title>
<script language="javas cript">
function DoSomething()
{
document.getEle mentById("div1" ).innerHTML =
"<input type='button' onclick='javasc ript:alert(\"He llo to you too!\");' value='Hit me' />";
}
</script>
</head>
<body>
<div id="div1"></div>
<input type="button" onclick="DoSome thing();" value="damn do something!">

</body>
</html>
[/html]

Ronald :cool:
Nov 15 '06 #2
sabesneit
4 New Member
Thank you but it's not exactly what i need. here another example, the first button change the div's javascript and the second button use the javascript function in the div.

try this, click the second button, the first and the second again.
the expected result is "no!" and "Hello world!" but i get "no!" 2 times even if changed the function DoIt() with the first button;

thank for your anwser

[HTML]
<html>
<head>
<title>this doesnt work!</title>
<script language="javas cript">
function DoSomething()
{
document.getEle mentById("div1" ).innerHTML = "!<script language=\"java script\">\n function DoIt(){\nalert( \"hello world\");\n}\n <\/script>!";
}
</script>
</head>
<body>
<div id="div1">
#<script language="javas cript">
function DoIt()
{
alert("no!");
}
</script>#
</div>
<input type="button" onClick="DoSome thing();" value="Change function"><br>
<input type="button" onClick="DoIt() ;" value="Button, Say hello">
</body>
</html>
[/HTML]
Nov 16 '06 #3
pronerd
392 Recognized Expert Contributor
The problem you are running into is that JS functions are treated like objects not text. An elements event handlers, in this case onclick, are set on page load so over writing the text after the page has loaded is not going to work for you.

If you want to change an elements assigned event handler you need to do something like this

<input type="button" id="button1" onClick="change Event();" value="Change function"/><br />


<script language="javas cript">
function changeEvent();
{
document.getEle mentById("butto n1").onclick = new Function("funct ion DoIt(){ alert(\'Event Changed\');}";
}
</script>
Nov 16 '06 #4

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

Similar topics

5
8442
by: Phil N | last post by:
Hello again Site I'm working on: http://www3.telus.net/bikim/lightning/test/ - 'coach' & 'main' links swap innerHTML of div element - id 'textArea' works fine in NN7; IE6 reports 'Object doesn't support this property or method' code is:
9
8656
by: Hallvard B Furuseth | last post by:
Why does the FAQ (Q 4.15) recommend innerHTML when so many here say one should use createElement(), replaceChild() etc? Also, why does the "Alternative DynWrite function" at <http://www.jibbering.com/faq/faq_notes/alt_dynwrite.html> need such a lot of tests to find out if innerHTML assignment actually works, instead of just inserting <span id="strange name"></span> and checking if the document now contains an element with that ID?
4
5124
by: tcole6 | last post by:
My problem appears to be Firefox specific. I have a hyperlink that loads a new window. This window contains hyperlinks that call javascript functions in the parent window and then closes the child window. The function that is called contains an XMLHttpRequest. My problem is that everything happens as it should, the innerHTML is changed by the results of the XMLHttpRequest and the child window closes. The problem is this, in Firefox,...
17
34701
by: PJ | last post by:
Greetings... I have stumbled upon a small problem. I use Ajax to retrieve part of a page I need to update. I update a DIV element with the HTML contents I get from another page. It works fine. However the HTML have a SCRIPT tag that the browser should process, but
2
2725
by: ars | last post by:
hi everyone i'm using some regular expression to paging, it work's fine in IE but not in Firefox i removed every thing to detect problem but i Can't , the only thing i got, is the innerHtml doent work as it should here is my page: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type"...
7
38002
by: John | last post by:
Hi Everyone, I'm having this extremely annoying problem with Internet Explorer 6, giving me an error message saying "unknown runtime error" whenever I try to alter the contents of a <divelement using innerHTML. Now, I've researched this problem on the web, and found many references to it, but none of them quite addressed my specific situation, and since my experience with JavaScript is limited, I was not able to adapt the solutions I...
8
3213
by: Pratik Patel | last post by:
Hello, I used innerHTML to assign HTML content. but in my HTML page content have also some javascript function and it will run when page load. bu when HTML code assgin thru innerHTML then this javascript is not working. e.g. html content:
15
7010
by: rage3324 | last post by:
I am posting html onto my main page between div tags using xmlhttprequest and innerhtml. The html I am posting has javascript inside which I am executing using the eval() function. However, the problem I face is that the javascript uses innerhtml as well and will not work for some reason. I have tested the javascript and it is definitely executing, it just will not change the innerhtml. To be more clear, the innerhtml is trying to access...
3
1824
by: BeginnerHyun | last post by:
A question on "innerHTML". Hi, The below is my HTML code. ActiveX function "GetData()" returns HTML string which may include javascript codes. This HTML string may be HTML codes in any pages which I will navigate to. The result of the below code is not what I expected. Error occurred in the line "divEncId.innerHTML=enc;".
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8802
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8465
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6158
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2699
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.