-2

I am iterating loop over Div and in that div assigning encoded array as value to hidden field and want to get value of that hidden field in each loop but getting undefiened

var port_ofAir = null; $(".sublocation_div").find('.sublocation').each(function(index,value1){
    port_ofAir = $(this).find(".port_arr").value;

  });
  console.log(port_ofAir)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" name="arr_port" class="port_arr" id="arr-port" value="[{"id":34,"client_id":"2"}]">

I want to get that array in jquery function

please any suggestion

JSmith
  • 4,519
  • 4
  • 29
  • 45
user7596840
  • 137
  • 15
  • try `.val()'. not sure but try it – JSmith Aug 29 '18 at 12:09
  • You would use `.val()` in jQuery to get the value of a form element. Though that particular form element looks like broken HTML syntax. The `value` there is just `"[{"`, everything else is outside of the `value` attribute. – David Aug 29 '18 at 12:12
  • can you show us the `div` element that has class of `sublocation_div` ? – Ariel Pepito Aug 29 '18 at 12:14

2 Answers2

0

In jQuery you need to use .val() instead of .value:

port_ofAir = $(this).find(".port_arr").val();
Scimonster
  • 32,893
  • 9
  • 77
  • 89
0

Try this: you need to use .val(), and not value. Also, you can put a specific jQuery selector for hidden fields using input[type=hidden]

$(".sublocation_div").find('.sublocation').each(function(index,value1){
    var port_ofAir = $(this).find("input[type=hidden].port_arr").val();
 }
Martin
  • 2,326
  • 1
  • 12
  • 22
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57