If you must do this with onclick
then I'd suggest:
<input type="button" class="gnM" onclick="$('#gender').val($(this).val())" value="male" />
<input type="button" class="gnM" onclick="$('#gender').val($(this).val())" value="female" />
JS Fiddle demo.
I'd strongly suggest moving from an onclick
to a jQuery unobtrusive method, using the click()
method:
$('.gnM').click(
function(){
var that = $(this);
$('#gender').val(that.val());
that.css('background-position','bottom right');
});
JS Fiddle demo.
To allow for toggling:
$('.gnM').click(
function(){
var that = $(this);
$('#gender').val(that.val());
that.siblings().removeClass('active');
that.toggleClass('active');
});
JS Fiddle demo.
Or, more concisely:
$('.gnM').click(
function(){
var that = $(this);
$('#gender').val(that.val());
that.toggleClass('active').siblings().removeClass('active');
});
JS Fiddle demo.
References: