Reputation: 137
I want to add specific id on radio button click function because I have 2 radio button divs. This is my current code :
HTML
<div id="radioGroup1" style="font-size:1em;">
<input type="radio" name="chooseBentukUsaha" id='chkperseorangan' value="Perseorangan" class="radiobentukusaha" checked> Perseorangan
<input type="radio" name="chooseBentukUsaha" id='chkpt' value="checkGroup" class="radiobentukusaha"> PT
<input type="radio" name="chooseBentukUsaha" id='chkcv' value="CV" class="radiobentukusaha"> CV
</div>
<div id="radioGroup2" style="font-size:0.8em;">
<input type="radio" name="choose" id='chkusaha' value="Pengembangan Usaha" class="radiopurpose" checked> Modal Usaha
<input type="radio" name="choose" id='chkpengembangan' value="Pengembangan Usaha" class="radiopurpose"> Pengembangan Usaha
<input type="radio" name="choose" id='chkstartup' value="Start Up" class="radiopurpose"> Penambahan Stok
<input type="radio" name="choose" id='chklain' value="Lain-lain" class="radiopurpose"> Ekspansi
<input type="radio" name="choose" id='chklain' value="Lain-lain" class="radiopurpose"> Lain-lain
</div>
My current jquery
$('input[type="radio"]').click(function() {
var inputValue = $(this).attr("value");
var targetBox = $("." + inputValue);
$(".box").not(targetBox).hide();
$(targetBox).show();
});
What I have tried
$('input[type="radio",id="radioGroup1"]').click(function() {
var inputValue = $(this).attr("value");
var targetBox = $("." + inputValue);
$(".box").not(targetBox).hide();
$(targetBox).show();
});
Upvotes: 1
Views: 418
Reputation: 422
Here I have added the solution it will also work if element will be created dynamically.
HTML
<div id="radioGroup1" style="font-size:1em;">
<input type="radio" name="chooseBentukUsaha" id='chkperseorangan' value="Perseorangan" class="radiobentukusaha" checked> Perseorangan
<input type="radio" name="chooseBentukUsaha" id='chkpt' value="checkGroup" class="radiobentukusaha"> PT
<input type="radio" name="chooseBentukUsaha" id='chkcv' value="CV" class="radiobentukusaha"> CV
</div>
<div id="radioGroup2" style="font-size:0.8em;">
<input type="radio" name="choose" id='chkusaha' value="Pengembangan Usaha" class="radiopurpose" checked> Modal Usaha
<input type="radio" name="choose" id='chkpengembangan' value="Pengembangan Usaha" class="radiopurpose"> Pengembangan Usaha
<input type="radio" name="choose" id='chkstartup' value="Start Up" class="radiopurpose"> Penambahan Stok
<input type="radio" name="choose" id='chklain' value="Lain-lain" class="radiopurpose"> Ekspansi
<input type="radio" name="choose" id='chklain' value="Lain-lain" class="radiopurpose"> Lain-lain
</div>
This is solution for dynamic created element also
var i = 1;
$('input[type="radio"]').on('click',function() {
alert('Its me!');
var element = "youId_"+i;
var inputValue = $(this).attr("id",element);
i++;
});
Upvotes: 1
Reputation: 16575
You can select it by id with input[type="radio"][id="chkperseorangan"]
not input[type="radio",id="radioGroup1"]
and you used parent id
not radio
:
$('input[type="radio"][id="chkpt"]').click(function() {
alert('Its me!');
//var inputValue = $(this).attr("value");
//var targetBox = $("." + inputValue);
//$(".box").not(targetBox).hide();
//$(targetBox).show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="radioGroup1" style="font-size:1em;">
<input type="radio" name="chooseBentukUsaha" id='chkperseorangan' value="Perseorangan" class="radiobentukusaha" checked> Perseorangan
<input type="radio" name="chooseBentukUsaha" id='chkpt' value="checkGroup" class="radiobentukusaha"> PT
<input type="radio" name="chooseBentukUsaha" id='chkcv' value="CV" class="radiobentukusaha"> CV
</div>
<div id="radioGroup2" style="font-size:0.8em;">
<input type="radio" name="choose" id='chkusaha' value="Pengembangan Usaha" class="radiopurpose" checked> Modal Usaha
<input type="radio" name="choose" id='chkpengembangan' value="Pengembangan Usaha" class="radiopurpose"> Pengembangan Usaha
<input type="radio" name="choose" id='chkstartup' value="Start Up" class="radiopurpose"> Penambahan Stok
<input type="radio" name="choose" id='chklain' value="Lain-lain" class="radiopurpose"> Ekspansi
<input type="radio" name="choose" id='chklain' value="Lain-lain" class="radiopurpose"> Lain-lain
</div>
Upvotes: 2
Reputation: 137
figured out by myself using this code :
$('input[name="chooseBentukUsaha"]').click(function() {
var inputValue = $(this).attr("value");
var targetBox = $("." + inputValue);
$(".box").not(targetBox).hide();
$(targetBox).show();
});
Upvotes: 1