1

I have two input fields with names "JobRoles[0].AgeFrom" and "JobRoles[1].AgeFrom". How can I access "JobRoles[0].AgeFrom" through jquery

I tried to access the field like this

 $(options.form).find(':input[name=' + fullOtherName + ']')[0];

but it show unrecognized expression: :input[name=JobRoles[0].AgeFrom]

Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52
Saneesh kunjunni
  • 548
  • 5
  • 17

2 Answers2

1

wrap your variable inside ". it will work.

var fullOtherName = 'JobRoles[0].AgeFrom';

alert($(document).find('input[name="' + fullOtherName + '"]').val());

var fullOtherName = 'JobRoles[0].AgeFrom';

alert($(document).find('input[name="' + fullOtherName + '"]').val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="JobRoles[0].AgeFrom" value="123" />
Amit Kumar
  • 5,888
  • 11
  • 47
  • 85
0

You can use contains selector on the input name field

like $("input[name*='AgeFrom']" and access the first element ([0])

See below snippet :

$(function(){
  var fullOtherName = 'AgeFrom';
  console.log($("input[name*='AgeFrom']"));
  alert($("input[name*='"+fullOtherName+"']")[0].value);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="JobRoles[0].AgeFrom" value="0"/>
<input name="JobRoles[1].AgeFrom" value="1"/>
Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52