I have the following code snippet embedded into some of my divs so when those divs get clicked a certain radio button gets checked.
onclick="document.g1.city[0].checked=true;"
However I would like to convert the above call to a function call like below:
onclick="checkRadioButton(city[0]);"
And the function will be something like this
function checkRadioButton(input){
document.g1.input.checked=true;
}
Is there a way I can accomplish this?
You can write any Javascript code inside the
onclickattribute. Remember thatcity[0]is not defined anywhere. To access it, you must specify the fulldocument.g1.city[0]. So the onclick becomes:Inside your function, you are already receiving the element, and don’t have to retrieve it from the document again. You could directly set it’s
checkedproperty: