Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
    I want to assign the radio button value from javascript variable, in inline html. Because I have lot of radio button, and I have a situation to save the radio button name in localstorage, so should I need to give the radio button name unique.If I change every radio button name manually, it take more time.

I have tried
HTML
<script>
var sampe=="XXID";
</script>

<input type="radio" name='"javascript:sample"+n1'/>


I have plan to using unique name on the every html page.
Posted

1 solution

Radio buttons are used when you want to let the user select only ONE option from a set of alternatives. For this to work, related radio buttons must have the same name to identified them as a group. e.g.
XML
<form>
<input type="radio" name="gender" value="male" checked>Male
<br>
<input type="radio" name="gender" value="female">Female
</form>

If you wan to assign name attribute to the radio button group programmatically, try this:
XML
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
   $("input:radio").attr("name","gender");
});
</script>
</head>
<body>

<form>
<input type="radio"  value="male" checked>Male
<br>
<input type="radio"  value="female">Female
</form>

</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900