![]() |
Button id passing value to javascript
Is it possible to create a numeric id to asign to buttons (images) generated in a loop and pass that numeric id (the number part) to a function in javascript?
Something like this: 1st line: Code:
<input id="q0" type="image" src="http://myurl/button-subtract.png" onclick="SubtractOne(0)"/> // (0) comes from q0Code:
<input id="q1" type="image" src="http://myurl/button-subtract.png" onclick="SubtractOne(1)"/> // (1) comes from q1 |
your code is illegal: two controls may not possess the same value for their id attribute, as all IDs must remain unique within the document.
if you must, use the name attribute to group controls, as it does not need to be unique. what you want to do is certainly possible, but not necessary, as you may accomplish the same by passing 'this', as in '...onclick="SubtractOne(this)" ', where the entire tag itself is passed to the javascript function. the function may then retrieve the id from the object itself. Observe: function SubtractOne(Caller){ alert( "the caller's id is " + Caller.getAttribute('id') ); } |
It worked. I only had to change all tags and passing the actual index to the function using
this:Code:
function AddOne(Caller){Code:
<input id="quantsubtract" type="image" src="http://myurl/button-subtract.png" onclick="SubtractOne(this)"> |
| All times are GMT +1. The time now is 01:51 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.