Nida Amin
Nida Amin

Reputation: 755

Last-child not working inside iteration of div

In browser, the HTML is generated dynamically and is rendered as

        <div id="dynamic-relationship-details">

        <div id="count-status0" class="relationship-container form-group">
              <div class="col-sm-1"></div>
                  <div class="col-sm-2">
                       <select id="relationship-type0" class="form-control"><option value="">Select Relationship</option><option value="Father">Father</option><option value="Mother">Mother</option><option value="Brother">Brother</option><option value="Sister">Sister</option><option value="Spouse">Spouse</option><option value="Guardian">Guardian</option></select>
                      </div>
                      <div class="col-sm-3">
                        <input type="text" name="relationship-type-name0" id="relationship-type-name0" class="form-control" placeholder="Name"></div><div class="col-sm-2"><input type="text" name="relationship-type-contact0" id="relationship-type-contact0" class="form-control" placeholder="Contact Number">
           </div>
               <button value="count-status0" class="remove-relationship-field btn btn-danger"><i class="fa fa-trash"></i></button>
           </div><div id="count-status1" class="relationship-container form-group">
          <div class="col-sm-1"></div>
          <div class="col-sm-2">
             <select id="relationship-type1" class="form-control"><option value="">Select Relationship</option><option value="Father">Father</option><option value="Mother">Mother</option><option value="Brother">Brother</option><option value="Sister">Sister</option><option value="Spouse">Spouse</option><option value="Guardian">Guardian</option></select>
          </div>
        <div class="col-sm-3">
              <input type="text" name="relationship-type-name1" id="relationship-type-name1" class="form-control" placeholder="Name"></div><div class="col-sm-2"><input type="text" name="relationship-type-contact1" id="relationship-type-contact1" class="form-control" placeholder="Contact Number">
       </div>
         <button value="count-status1" class="remove-relationship-field btn btn-danger"><i class="fa fa-trash"></i></button>
     </div>
     </div>

The code is

       // Relationship details Array
          $(".relationship-container").each(function(i, obj) {
                 var $this = $(this);
                 $this.find("select").each(function() {

                        var relationshipTypeValue = $(this).val();
                        var relationshipName =$this.find("input[type=text]:first-child").val();
                        var relationshipContactNumber =$this.find("input[type=text]:last-child").val();

                        var innerRelationshipArray = {};
                             innerRelationshipArray = {
                              relationshipTypeValue: relationshipTypeValue,
                              relationshipName: relationshipName,
                              relationshipContactNumber: relationshipContactNumber
                             };

                        relationship_details_array.push(innerRelationshipArray);

                      });
              });

I am trying to fetch values of contact numbers i.e with id's relationship-type-contact(n) values in the line "

var relationshipContactNumber =$this.find("input[type=text]:last-child").val();"

This is fetching values of first-child textboxes in the variable relationshipContactNumber in the loop.

Please help !!!

Upvotes: 0

Views: 47

Answers (1)

KungWaz
KungWaz

Reputation: 1956

This should work, use the .first() and .last() selectors.

Another way, if you have access to alter the HTML, is to add class names for the input fields and select them by using that instead.

function getData() {
	var relationship_details_array = [];

  // Relationship details Array
  $(".relationship-container").each(function(i, obj) {
    var $this = $(this);

    $this.find("select").each(function() {
      var relationshipTypeValue = $(this).val();
      var relationshipName = $this.find("input[type=text]").first().val();
      var relationshipContactNumber = $this.find("input[type=text]").last().val();

      var innerRelationshipArray = {};
      innerRelationshipArray = {
        relationshipTypeValue: relationshipTypeValue,
        relationshipName: relationshipName,
        relationshipContactNumber: relationshipContactNumber
      };

      relationship_details_array.push(innerRelationshipArray);

    });
  });

  console.log(relationship_details_array);
}

$("#getData").on("click", getData);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="getData">Show data in console log</button>
 <div id="dynamic-relationship-details">
   <div id="count-status0" class="relationship-container form-group">
     <div class="col-sm-1"></div>
     <div class="col-sm-2">
       <select id="relationship-type0" class="form-control">
         <option value="">Select Relationship</option>
         <option value="Father">Father</option>
         <option value="Mother" selected>Mother</option>
         <option value="Brother">Brother</option>
         <option value="Sister">Sister</option>
         <option value="Spouse">Spouse</option>
         <option value="Guardian">Guardian</option>
       </select>
     </div>
     <div class="col-sm-3">
       <input type="text" name="relationship-type-name0" id="relationship-type-name0" class="form-control" value="Mommy" placeholder="Name">
     </div>
     <div class="col-sm-2">
       <input type="text" name="relationship-type-contact0" id="relationship-type-contact0" class="form-control" value="1234" placeholder="Contact Number">
     </div>
   </div>
   <div id="count-status1" class="relationship-container form-group">
     <div class="col-sm-1"></div>
     <div class="col-sm-2">
       <select id="relationship-type1" class="form-control">
         <option value="">Select Relationship</option>
         <option value="Father" selected>Father</option>
         <option value="Mother">Mother</option>
         <option value="Brother">Brother</option>
         <option value="Sister">Sister</option>
         <option value="Spouse">Spouse</option>
         <option value="Guardian">Guardian</option>
       </select>
     </div>
     <div class="col-sm-3">
       <input type="text" name="relationship-type-name1" id="relationship-type-name1" class="form-control" value="Daddy" placeholder="Name">
     </div>
     <div class="col-sm-2">
       <input type="text" name="relationship-type-contact1" id="relationship-type-contact1" class="form-control" value="5678" placeholder="Contact Number">
     </div>
   </div>
</div>

Upvotes: 1

Related Questions