Pritish
Pritish

Reputation: 767

Radio button to populate text fields

I'd the foll. requirement for one of my web applications:

In my HTML table, some of the columns require user input as textfields. Next to the text field, I wanted to have a radio button on each row which populates rows meeting certain criteria in my table.

Here's my code

   <td> <input type="text" id="newContact<%=rCount %>" name="newContact">
   <br>
   <input type="radio" id="applyNewContact<%=rCount %>" name="applyNewContact" onchange=..some js function here..>     
   <label for="applyNewContact<%=rCount %>">Apply Contact to all cases @Inst.ID</label> 
   </td>
   <td align="center"> <input type="text" id="newContactPhone<%=rCount %>" name="newContactPhone"></td>
   <td align="center"> <input type="text" id="newContactEmail<%=rCount %>" name="newContactEmail"></td>

So, if radio button on row 1(with id applyNewContact0) was checked, then I wanted to populate all rows(only their newContact, newContactPhone, newContactEmail) with Inst.ID(one of my tables columns) same as row1

How would I go about doing this using jQuery or Javascript?

Thanks,
Pritish

Upvotes: 0

Views: 424

Answers (1)

Scott Evernden
Scott Evernden

Reputation: 39940

something like this?

var $radios = $(':radio[id^=applyNewContact]').change(function() {
   var id = $radios.index(this);
   $('#newContact' + id).val(inst[id].newContact);
   $('#newContactPhone' + id).val(inst[id].newContactPhone);
   $('#newContactEmail' + id).val(inst[id].newContactEmail);
});

this assumes rCount is a 0+ counter. if not replace

  var id = this.id.match(/applyNewContact(.+)/)[1];

Upvotes: 1

Related Questions