Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  15] [ 4]  / answers: 1 / hits: 16615  / 11 Years ago, wed, october 2, 2013, 12:00:00

I have two set of radio button in my html form and I am displaying a hidden div depends upon the radio button selection.And the hidden div ONLY display if both radio buttons are clicked.I am using a jquery to trigger click if the radio buttons are clicked



But when ever I load the page, the buttons are getting checked as expected and it not triggering the click event. due to which a user has to check the radio buttons again(eventhough they are selected).



My html code for radio buttons:



<div class=block>
<input onClick=show_seq_lunid(); type=radio name=button1 value=Yes <?php if(!isset($_POST['button1']) || (isset($_POST['button1']) && $_POST['button1'] == 'Yes')) echo ' checked=checked'?> checked /><label>Yes</label>
<input onClick=show_list_lunid(); type=radio name=button1 value=No <?php if(isset($_POST['button1']) && $_POST['button1'] == 'No') echo ' checked=checked';?> /><label>No</label>
</div>

<div class=block>
<input onClick=os_others(); type=radio name=button2 value=Yes <?php if(!isset($_POST['button2']) || (isset($_POST['button2']) && $_POST['button2'] == 'Yes')) echo ' checked=checked'?> checked /><label>Others</label>
<input onClick=os_hpux(); type=radio name=button2 value=No <?php if(isset($_POST['button2']) && $_POST['button2'] == 'No') echo ' checked=checked';?> /><label>HP-UNIX</label>
</div>


And I tried below code to trigger click event for a checked radio button



$('input:radio[name=button1]:checked').click();
$('input:radio[name=button2]:checked').click();


but it is not working. How can I trigger the click event ?



Update:
Tried in DOM ready mode using code below, but no luck



$(document).ready(function() {
$('input:radio[name=button1]:checked').click();
$('input:radio[name=button2]:checked').click();
});

More From » php

 Answers
7

http://jsfiddle.net/tVRrb/



if($('input[name=button1]').is(':checked')){
alert('1 is checked');
}

if($('input[name=button2]').is(':checked')){
alert('2 is checked');
}


The above works, but I noticed that jsfiddle doesnt like function calls in the onClick attribute. The below worked in my browser but not on jsfiddle. It doesnt see the function being defined somehow.



<!DOCTYPE html>
<html>
<head>
<script src=http://code.jquery.com/jquery-1.9.1.min.js></script>
</head>
<body>

<div class=block>
<input onClick=myOtherAlert(); type=radio name=button1 value=Yes checked /><label>Yes</label>
<input onClick=myOtherAlert(); type=radio name=button1 value=No /><label>No</label>
</div>

<div class=block>
<input onClick=myAlert(); type=radio name=button2 value=Yes checked /><label>Others</label>
<input onClick=myAlert(); type=radio name=button2 value=No /><label>HP-UNIX</label>
</div>


<script>
if($('input[name=button1]').is(':checked')){
$('input[name=button1]:checked').trigger('click');
}

if($('input[name=button2]').is(':checked')){
$('input[name=button2]:checked').trigger('click');
}

function myAlert(){
alert('it worked');
}
function myOtherAlert(){
alert('see');
}
</script>
</body>
</html>

[#75277] Tuesday, October 1, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
georginaariao

Total Points: 626
Total Questions: 112
Total Answers: 112

Location: Burkina Faso
Member since Thu, Dec 15, 2022
2 Years ago
;