1

I have this input field

<input name="Ops[2][Duration]" type="text" value="1" row-number="2">

Which I'm trying to select by name but it seems that

$('input[name=Ops[2][Duration]]');

Gets confused with all the brackets. How can I sort this out ? I tried

$('input[name=Ops\[2\]\[Duration\]]');

But that didn't work either, I still get:

Error: Syntax error, unrecognized expression: input[name=Ops[2][Duration]]

Connor Bishop
  • 921
  • 1
  • 12
  • 21
  • You have missed to wrap the attribute value with double quotes,it as to be like this $('input[name="Ops[2][Duration]"]'); – Lokesh_Ram Jul 25 '16 at 10:58

3 Answers3

2

In a jQuery selector the escape character is two backslashes: \\:

$('input[name=Ops\\[2\\]\\[Duration\\]]');

Alternatively you could put the attribute value in quotes:

$('input[name="Ops[2][Duration]"]');
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
2

You can wrap the attribute value in quotes:

$('input[name="Ops[2][Duration]"]');
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1

Use the name attribute value in paired quotes

$('input[name="Ops[2][Duration]"]');

or

$("input[name='Ops[2][Duration]']");

$(function(){
    $('input[name="Ops[2][Duration]"]').on('blur', function(){
        console.log($(this).val());
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="Ops[2][Duration]" type="text" value="1" row-number="2">
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400