0
$(function(){
   function First(){
        $(this).next().attr("src", "point.png");
   }
});

This code isnt working, can you help me?

Here is html:

         Username: <input type="text" name="username" onkeypress="First()">
     <img src="" class="vimg">
     <br>
2

1 Answer 1

1

It's because your function First is scoped within the anonymous function. In order for onkeypress="First()" to work, it must be a global function.

I generally prefer to scope my functions as you have (to prevent polluting the global namespace), so what most people do in this case is to set the onkeypress event through JavaScript (from inside your anonymous method). It would look something like this:

$(function(){
   function First(){
        $(this).next().attr("src", "point.png");
   };

   $("input[name='username']").on('keypress', First);
});
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.