0

I would checked radio with javascript. But i can't.

I tried :

document.forms["formisim"]["radio1id"].checked = true;

document.formisim.radio1name.value = "indirimli";

$("#radio1id").prop("checked", true);

HTML code

   <form name="formisim">
    <input type="radio" 
           class="indirimsizb" 
           name="radio1name" 
           id="indirimsizid" 
           value="indirimsiz" 
           onchange="iindirim()"
           checked> 
    <input type="radio" 
           class="indirimlib" 
           name="radio1name" 
           id="radio1id" 
           value="indirimli"
           onchange="iindirim()">
</form>

How can i resolve this?

I need your help.

brk
  • 48,835
  • 10
  • 56
  • 78

4 Answers4

0

this is javascript code -

document.getElementById("radioid").checked = true;

this is jquerycode -

$("#radioid").prop("checked", true);
0

Use name attribute selector

document.forms[name = "formisim"]["radio1id"].checked = true;
<form name="formisim">
  <input type="radio" class="indirimsizb" name="radio1name" id="indirimsizid" value="indirimsiz" onchange="iindirim()" checked>
  <input type="radio" class="indirimlib" name="radio1name" id="radio1id" value="indirimli" onchange="iindirim()">
</form>
brk
  • 48,835
  • 10
  • 56
  • 78
0

All the methods that you tried will work.

1) Using the document.forms collection:

document.forms["formisim"]["radio1id"].checked

2) Using the IDs as properties. Notice that you must use the ID, not the name like you did. The form name can be used, though. Also for some reason, you used the value here instead of checked. Both will work, but give you different values of course:

document.formisim.radio1id.value

3) Using jQuery with ID selector:

$("#radio1id").prop("checked")

4) Also, similar to jQuery above, you can use getElementById() with the ID:

document.getElementById("radio1id").checked

Here is a demo of all these methods:

console.log(document.forms["formisim"]["radio1id"].checked);
console.log(document.formisim.radio1id.value);
console.log($("#radio1id").prop("checked"));
console.log(document.getElementById("radio1id").checked);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="formisim">
  <input type="radio" class="indirimsizb" name="radio1name" id="indirimsizid" value="indirimsiz" onchange="iindirim()" checked>
  <input type="radio" class="indirimlib" name="radio1name" id="radio1id" value="indirimli" onchange="iindirim()">
</form>
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
0

check this snippet working on radio button

$("#radio1id").attr("checked", true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="formisim">
    <input type="radio" 
           class="indirimsizb" 
           name="radio1name" 
           id="indirimsizid" 
           value="indirimsiz" 
           onchange="iindirim()"
           checked> <text>asd</text>
    <input type="radio" 
           class="indirimlib" 
           name="radio1name" 
           id="radio1id" 
           value="indirimli"
           onchange="iindirim()">
           <text>bsd</text>
</form>
jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22