Brian Schroeter
Brian Schroeter

Reputation: 1633

Selecting Radio Option based on Query String

I'm having some trouble with selecting the radio option affiliated to the query string. I have the following code in place...

<?php if ( $_GET['fp'] == 'floorplanfive' ) { ?>
  <script type="text/javascript">
    jQuery('#choice_2_4').attr('checked', 'checked');
    console.log("test");
  </script>
<?php } ?>

The console is showing the message 'test' but the radio option is not being selected.

The page can be found here.

Please help out. :-)

Upvotes: 1

Views: 312

Answers (4)

ahoo
ahoo

Reputation: 1361

Use this:

jQuery('#choice_2_4').prop('checked', true);

Edit: if your radio button is loaded after this code, use it:

jQuery(document).ready(function () {
    jQuery('#choice_2_4').prop('checked', true);
}

Upvotes: 1

Freelancer
Freelancer

Reputation: 9074

Try Following:

jQuery('#choice_2_4').attr('checked', true).checkboxradio("refresh");

Upvotes: 0

billyonecan
billyonecan

Reputation: 20270

You need to put your code inside of the $(document).ready() function:

jQuery(document).ready(function() {
  // your code
});

Upvotes: 1

Faisal Sayed
Faisal Sayed

Reputation: 793

Use == comparison operator instead of = assignment.

Assignment in if condition will always return true.

Change your code to:

<?php if ( $_GET['fp'] == 'floorplanfive' ) { ?>
  <script type="text/javascript">
    jQuery('#choice_2_4').attr('checked', 'checked');
    console.log("test");
  </script>
<?php } ?>

Upvotes: 0

Related Questions