Reputation: 557
So I have this Ruby on Rails application where I open a modal, to make a new user. Now I want to listen to the forms in that modal with javascript, but for some reason it will not find my elements at all.
So lets say I have this function in .js:
$(document).ready(function()
{
$("#PWD1").on("keypress keyup keydown", function()
{
console.log("Check");
});
});
And this is how my input field looks like in the modal:
<input
class="PWD1"
id="user_password"
name="user[password]"
placeholder="Password..."
type="password">
And now when I type anything in that input field, my Check never gets printed into the console. Why is that?
Upvotes: 0
Views: 53
Reputation: 74738
change to this:
$(".PWD1")
or with id:
$("#user_password")
You need to do a class selector this way.
You were referencing your selector with a id while that is a class name.
And also i am not sure if your modal is dynamically generated then you have to delegate the event to the closest static parent or $(document)
itself.
$(document).on("keypress keyup keydown", ".PWD1", function() {
As you are using keypress keyup keydown
events so the callback function will trigger twice because of keyup and keydown,keypress
.
Upvotes: 1