Shyam Dixit Shyam Dixit - 1 year ago 48
Javascript Question

Storing value of radio button in Local Storage

I think this is very silly question but i didn't get it.Actually I am trying to get values of checked radio button and store all to local Storage but it is showing null.

My HTML code is:

<div id="radioMediators" style="display:none; float:left; margin-left:50px;margin-top:8px;">
<input type="radio" name="M1P1" value="1">1
<input type="radio" name="M1P1" value="2">2
<input type="radio" name="M1P1" value="3">3
<input type="radio" name="M1P1" value="4">4
<input type="radio" name="M1P1" value="5">5
<br/><br/>
<input type="radio" name="M1P2" value="1">1
<input type="radio" name="M1P2" value="2">2
<input type="radio" name="M1P2" value="3">3
<input type="radio" name="M1P2" value="4">4
<input type="radio" name="M1P2" value="5">5
<br/><br/>
<input type="radio" name="M1P3" value="1">1
<input type="radio" name="M1P3" value="2">2
<input type="radio" name="M1P3" value="3">3
<input type="radio" name="M1P3" value="4">4
<input type="radio" name="M1P3" value="5">5
<br/><br/>
<input type="radio" name="M1P4" value="1">1
<input type="radio" name="M1P4" value="2">2
<input type="radio" name="M1P4" value="3">3
<input type="radio" name="M1P4" value="4">4
<input type="radio" name="M1P4" value="5">5
<br/><br/>
<input type="radio" name="M1P5" value="1">1
<input type="radio" name="M1P5" value="2">2
<input type="radio" name="M1P5" value="3">3
<input type="radio" name="M1P5" value="4">4
<input type="radio" name="M1P5" value="5">5
</div>


And for storing purpose I am using this code:

var a1=localStorage.getItem($('input[name="M1P1"]:checked'));


Same for all(M1P2,M1P2,M1P3,M1P4,M1P5)

alert(a1);
alert(b1);


But this is showing null.
What I did wrong.Please let me know.

Thanks

Answer Source

I got it and found that it was a very silly question.

localStorage.setItem('a_value',$('input[name="M1P1"]:checked').val());

alert(localStorage.getItem('a_value'));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download