Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unrecognized expression: Selecting element with multiple classes

Tags:

jquery

It sure is an unrecognized expression, but how can i fix it?

 $('body').on('keydown', 'input[class=form-control amount]', function (e) {
     if (e.which === 110 | e.which === 190) {
         e.preventDefault();
         return false;
     }
});
like image 877
OrElse Avatar asked Jul 20 '26 23:07

OrElse


2 Answers

Try this:

$('body').on('keydown', 'input.form-control.amount', function (e) {
 if (e.which === 110 | e.which === 190) {
     e.preventDefault();
     return false;
 }
});
like image 120
Gagan Deep Avatar answered Jul 23 '26 16:07

Gagan Deep


You could add quotes around the class like so:

class="form-control amount"

to specify that the class attribute has 2 classes:

$('body').on('keydown', 'input[class="form-control amount"]', function (e) {
     console.log("key down");
     if (e.which === 110 | e.which === 190) {
         e.preventDefault();
         return false;
     }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" class="form-control amount" placeholder="logs"/>
<input type="text" class="foo" placeholder="no logs"/>
like image 40
Nick Parsons Avatar answered Jul 23 '26 17:07

Nick Parsons