473,386 Members | 1,821 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,386 software developers and data experts.

Why won't innerHTML allow me to write an onclick event?

Expand|Select|Wrap|Line Numbers
  1. document.getElementById('productThumbs').innerHTML=
  2.  
  3. "<p>Product 1</p>
  4. <p>Description of product 1</p>
  5. <img src='images/thumbs/product1.png'
  6. class='image'
  7. onclick='changepic('images/product1.png');'
  8. />";
  9.  
The contents of the <p> tags, and the image are displayed correctly. The onclick event isn't triggered.

Is there any way to discover if the class of 'image' has been applied to the image? And why doesn't the onclick function work?

Thanks
Jan 24 '11 #1
3 5761
Oralloy
988 Expert 512MB
spheric,

I'm shooting from the hip a little bit here.

Have you tried changing the "onclick" call to simply invoke the alert method? That way you can check whether the event is failing, or if the code is failing.

If the event is failing, you're going to have to add the onclick event after the fact, by starting with
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('productThumbs')
and then selecting out the img element by going to the childNodes
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('productThumbs').childNodes[5]
(NOTE: you may have to juggle the index a little to get the node number correct. I am not highly practiced in this regard, so you'll have to be patient with me)
And, finally adding an event handler...
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('productThumbs').childNodes[5].onclick = function(){alert('test me');}
As I said, just a sketch of how to get there.

Good Luck!
Oralloy
Jan 24 '11 #2
Rabbit
12,516 Expert Mod 8TB
Are the line breaks simply for our benefit?
Jan 24 '11 #3
Oralloy
988 Expert 512MB
spheric,

I also noticed that you have what looks like a syntax problem in your innerHTML.

Specifically, the quoting of the argument to changepic.

Try changing the quoted argument to double quotes, like so:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('productThumbs').innerHTML= 
  2.   "<p>Product 1</p><p>Description of product 1</p><img src='images/thumbs/product1.png' class='image' onclick='changepic(\"images/product1.png\");' />";
Cheers!
Oralloy
Jan 24 '11 #4

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

Similar topics

4
by: Dipin | last post by:
Hi All; I have this javascript which is adding a new button to the column in the row which is created dynamically, the innerhtml shows that the onclick event is correctly added but it never gets...
17
by: Mike Gratee | last post by:
Is it possible to use JavaScript to cause the browser to click a link on a page and have the browser act exactly like the user had clicked on the link directly? In other words, I need to...
10
by: WindAndWaves | last post by:
Hi Gurus I have the following code: function CI(ImgN, Alt){ if( document.getElementById && document.getElementById('il') ){ var d=document.getElementById('il');
17
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick...
4
by: Chad | last post by:
I have a link (.ascx) and that generates an Add form on that page. The autopostback dropdown is within a "If Not IsPostBack Then" statement. The form that is created is all via static html in the...
4
by: Ryan | last post by:
Hello, I have a standard HTML button on an aspx web form that I have set to runat server. The button is named reset1 and its tag is as follows: <INPUT id="btnReset1" style="WIDTH: 60px;...
1
by: saiyen | last post by:
Hey all, ive been banging my head agains the wall for a few days now, and finally decided to ask for help. i have a document that writes the header with all the javascript in it, then modifyes a...
11
by: Daz | last post by:
Hello everyone. I am sure the answer to my question is simple, but I can't seem to dynamically add an onClick event to my script. I have a table which is generated dynamically, I am just...
18
by: joaotsetsemoita | last post by:
Hello everyone, I'm having troubles assigning an onclick event to a cell. Im trying something like cursorPoint.cells.style.cursor = "hand"; cursorPoint.cells.width = "20";...
2
by: swethak | last post by:
Hi, i am getting the problem when i used the onclick event in option tag.It is working fine in mozilla .But it is not working IE. Here is my code <script>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.