Jason Hunn
Jason Hunn

Reputation: 41

Javascript if statement for if user has input type clicked

I have the following input type:

<input type="text" id="textid">

and I want to make it so if the user clicks it, or if it is currently clicked or selected, it will alert "clicked". I tried doing this, but it didn't work.

$('#textid').focus(function() {
    alert('Function is clicked.');
});

What am I doing wrong?

Upvotes: 0

Views: 44

Answers (2)

Van M. Tran
Van M. Tran

Reputation: 112

With that code, the alert form will be appeared consecutively. I think you should create a handle function before, then bind it when focusout and unbind it when focus. Demo

Upvotes: 1

Roko C. Buljan
Roko C. Buljan

Reputation: 206352

You're doing nothing wrong if:

you used:

<script src="https://code.jquery.com/jquery-2.1.4.js"></script>

and...

jQuery(function( $ ){ // (DOM is now ready and $ alias secured)

     // ...placed your code here

});

jsBin demo

Upvotes: 0

Related Questions