简体   繁体   中英

How to check the radio button in which id field is blank using javascript

Here is sample html code

<div class="radioOff">
    <input id="" class="hidden" type="radio">
    <label for="">already a member</label>
    <span class="identity">identify</span>
</div>

I tried using document.getElementById("").checked = true; its not working.

I can not do any change in code . Here is that third party website where I need to select radio button http://www.degriftour-selection.fr/login.html

Try:

$(".hidden[id='']").prop("checked",true);

DEMO here.

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>   
<div class="radioOff">
<input id="" class="hidden" type="radio">
<label for="">already a member</label>
<span class="identity">identify</span>
</div>
<script>
$('#document').ready(function () {
    $(".hidden").attr("checked", true);
});
</script>

Can you try this,

$(function(){
    $(".hidden").attr("checked", true);
});

You have used empty id="" , you can add id value or you can use to get using classname.

  <input id="myRadio" class="hidden" type="radio">

Jquery,

 $("#myRadio").attr("checked", true);

Javascript:

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

try this

$('document').ready(function () {
    $(".hidden").attr("checked", true);
});

There is no possibility to reference to empty ID because it'll be ignored in DOM.

One option is to use getElementsByClassName() to get all elements and then iterate through them. Second option (better) will be adding proper ID attribute and use it then.

You could do something like this, I believe the querySelector is supported from IE8+ and on all other major browsers.

var elem = document.querySelector(".radioOff"),
    radio = (elem.firstElementChild||elem.firstChild);

radio.checked = true;

JSFiddle

Updated according to comment. To select the checkbox déjà membr you could use the JQuery selector (as the page includes JQuery 1.7.2)

$('.radioOff').find('input[type=radio]').prop('checked', true);

I have tried this and it is working perfectly.... please try from your side...

<div class="radioOff">
    <input id="" class="hidden" type="radio" />
    <label for="">already a member</label>
    <span class="identity">identify</span>
</div>

and then after use below jquery code (you also need to add jQuery file reference)...

<script type="text/javascript" language="javascript">
$(document).ready(function () {
    $(".hidden").attr("checked", true);
});
</script>

You can make use of this code in ur js:

return ($('input[type=radio]:checked').size() > 0);

Or

if(document.getElementsByClassName("hidden").checked)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM