I would like to build two buttons with an onclick function that will insert “male” or “female” into another input.
The two buttons have background images and need to change background-position when clicked. I need both buttons to toggle from each other.
Can anyone suggest a solution with javascript or jquery?
<input type="button" class="gnM" onclick="???????">
<input type="button" class="gnF" onclick="???????">
<input class="req-string gender" id="gender" name="gender">
If you must do this with
onclickthen I’d suggest:JS Fiddle demo.
I’d strongly suggest moving from an
onclickto a jQuery unobtrusive method, using theclick()method:JS Fiddle demo.
To allow for toggling:
JS Fiddle demo.
Or, more concisely:
JS Fiddle demo.
References:
click().css().removeClass().siblings().toggleClass().val().