Soner
Soner

Reputation: 37

text input change class on mouse click

<input type="text" onkeypress="return buyukHarf(event);" class="green" name="f1"/>

is it possible to change the class green to something else when the area is clicked? Or any other way to change the border color when clicked? Thanks in advance...

Upvotes: 0

Views: 870

Answers (3)

KiiroSora09
KiiroSora09

Reputation: 2287

Try this:

$( "input" ).click( function() {
    $( this ).attr( { 'class' : 'anotherClass' } );
} );

Upvotes: 2

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

Try:

$("input[name='f1']").click(function(){
    $(this).removeClass("green").addClass("red");    
});

OR // for border

$("input[name='f1']").click(function(){
    $(this).removeClass("green").css("border", "1px solid red");    
});

Upvotes: 1

ahren
ahren

Reputation: 16959

$("input").click(function(){
    $(this).removeClass("green").addClass("yellow");    
});

In the case of form elements, I'd use focus() and blur() over a click() handler.

Also, try to avoid using inline javascript as you have done in your example.

Focus/Blur example:

$("input").focus(function(){
    $(this).removeClass("green").addClass("yellow");
}).blur(function(){
    $(this).removeClass("yellow").addClass("green");
});

Upvotes: 6

Related Questions