Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the id of a parent or closest fieldset?

HTML:

<fieldset id="10">
  <h1>Select a color:</h1>
  <select name="color">
    <option>red</option>
    <option>blue</option>
  </select>
</fieldset>

JS:

var fields = $(":input").serializeArray();

$.each(fields, function (i, field) {
  var field_id = $(field.name).closest("fieldset").attr('id');
  $("#results").append('<a href="'+field_id+'">'+field.value+'</a>');
});

All I get is undefined?

When I do: field.name, it gives me "color". I want to be able to find the parent or closest fieldset that this form element belongs to.

I expect field_id to equal "10" in this example.

like image 851
MrPizzaFace Avatar asked Nov 18 '25 08:11

MrPizzaFace


1 Answers

I do: field.name, it gives me "color"

Use

var field_id = $('[name=' + field.name+']').closest("fieldset").attr('id');
like image 130
Satpal Avatar answered Nov 20 '25 22:11

Satpal