Reputation: 31
I have this html generated by a prepopulated form in gravity forms:
<ul id="input_4_17" class="gfield_radio">
<li class="gchoice_17_0">
<input type="radio" tabindex="1" id="choice_17_0" name="input_17">
<label for="choice_17_0">
<img class="stock instock" id="gchoice_17_0">
</label>
</li>
<li class="gchoice_17_1">
<input type="radio" tabindex="1" id="choice_17_1" name="input_17">
<label for="choice_17_1">
<img class="stock outofstock" id="gchoice_17_1">
</label>
</li>
<li class="gchoice_17_2">
<input type="radio" tabindex="1" id="choice_17_2" name="input_17">
<label for="choice_17_2">
<img class="stock outofstock" id="gchoice_17_2">
</label>
</li>
</ul>
I'm trying to disable inputs based on the stock status, that is the class of the img element. I can't put this class in the input, so I'm using this javascript to disable inputs based on the class of the image to disable inputs:
$(document).ready(function () {
if ($('img#gchoice_17_0').hasClass('outofstock')) {
$('input#choice_17_0').attr("disabled", "disabled").prop("checked", false);
}
if ($('img#gchoice_17_1').hasClass('outofstock')) {
$('#choice_17_1').attr("disabled", "disabled").prop("checked", false);
}
if ($('img#gchoice_17_1').hasClass('outofstock')) {
$('#choice_17_1').attr("disabled", "disabled").prop("checked", false);
}
});
This works but I know this is not the best way to do this. I'm trying this code, but it doesn't work.
$(document).ready(function () {
if ($('img').hasClass('outofstock')) {
var getIde = $(this).attr("id");
$('input#' + getIde).attr("disabled", "disabled").prop("checked", false);
}
});
Does anyone have any ideas why it isn't working?
Upvotes: 2
Views: 356
Reputation: 656
try this :
$(document).ready(function(){
$('.outofstock').parent().prev().prop('disabled',true).prop('checked',false);
});
Upvotes: 2
Reputation: 146239
You may try this
$('.outofstock').parent().prev('input:radio').prop('disabled', 1)
Update :
$('.outofstock').parent().prev('input:radio').prop({
'disabled':1,
'checked':0
});
Upvotes: 1
Reputation: 4150
Give this a shot...
$( document ).ready( function(){
$('img').each(function(index){
if($(this).hasClass('outofstock')){
var getIde = $(this).attr('id');
$('#'+getIde).attr('disabled', 'disabled').prop('checked', false);
}
});
});
Upvotes: 0