views:

360

answers:

2

i have following code for radio buttons

how can make radio buttons to be selected when i click on any of two labels in front of that radio button.

<div class="label_main" style="width:350px">
    <div class="label_radio">
      <input type="radio" name="group1" id="group1" value="0" 
    <?php if($r0==0) { ?>
    checked="checked"
    <?php  } ?>
   />
    </div>
    <div class="label_top">
      <label for="group1">First Option </label>
    </div>
    <div class="label_desc">
      <label for="group1">This is first option </label>
    </div>
  </div>
      </div> 

  <div class="label_main" style="width:350px">
    <div class="label_radio">
      <input type="radio" name="group1" id="group1" value="1"
    <?php if($r0==1) { ?>
    checked="checked"
    <?php  } ?>
   />
    </div>
    <div class="label_top">
      <label for="group1">Second Option </label>
    </div>
    <div class="label_desc">
      <label for="group1">This is Second Option </label>
    </div>
  </div>

only problem i am having if i change radio ID it work fine, but i need same id cause i need its value at the end to save in database.

+3  A: 

You're confusing the name attribute with the id attribute.

They should each have the same name, but a different id.

For example:

<input type="radio" name="group1" id="group1_0" value="0">
<input type="radio" name="group1" id="group1_1" value="1">

<label for="group1_0">CLicky 0</label>
<label for="group1_1">CLicky 1</label>
Greg
A: 

ID and name of the input don't have to be the same. Leave the name and change the ID of the second radio to something else (and change the second label's "for" attribute to that ID). When you will submit the form to the server you will get the value by the "name" attribute. Server doesn't know anything about IDs.

Jan Hančič
i am not submitting form via Jquery and i am using id tag to get values...
air
jQuery (or JavaScript in general) doesn't have anything to do with this.id is not a tag, it's an attribute.And no, once you submit to the server you access values of fields via their name not id. Because, like I said, server doesn't know anything about the IDs.
Jan Hančič