For some reason I can have an input type as a button with an onclick event but when I set the input type as image and put an onclick event into it, it doesn't seem to work.

I.e.
This works:

<input type="button" onclick="func('hello')"/>

This doesn't work:

<input type="image" onclick="func('hello')" src="image.jpg"/>

Recommended Answers

All 5 Replies

Try changing the atrribute's place. For example,

<input type="image"  src="image.jpg"  onclick="func('hello')" />

Is the image showing? Is it in the same directory as your HTML file/files?

Look--
1. First of all, look in the JavaScript forum.
2. Onclick = wrong spelling; OnClick = correct spelling
3. Better write

"javascript:hello"

.
4. Image is NOT AN INPUT TYPE: easy way to make that work =

<a href="javascript:hello" ><img src="image.jp[B]e[/B]g width="" height="" alt="" border="0"></img></a>

yes, it is correct that onclick is wrong whereas OnClick is right. Javascript is case sensitive language and you have to pay extra attention while writing javascript predefined functions.

I suggest you check out this website. It will help you master the basics of HTML and JavaScript.

Please delete these mis-leading views. html attributes are defined in w3c as per doc types e.g. xhtml 1.1 strict, html5, xhtml 1.0 Transitional etc. "You can find this at the top of your document". Ideally all attributes are lower case i.e. onclick. Now the real issue you are finding here depends on the input type. If you test with input type submit and experience the same thing it is because your controls are in a form control. This is by design that html forms are submitted with those kind of controls. a return false in your onclick function may do the trick.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.