Change border-color according to the value of a jquery variable

Asked

Viewed 100 times

0

I have a form and want to change the color of the border-color to red if the user/email has already been registered in my database.

I tried something like:

<script>

        $(document).ready( function(){
            $('#btn_escrever').click(function(){
                if($(usuario_existe == 1){
                    $('#usuario').css({'border-color': '#A94442'});
                }

</script>

btn_write is the button I use to register; the usuario_existe is a variable I’m using in php to check if the user already exists or not in bd, if it has value 1, it is because it already exists. Something’s wrong, someone can help me?

  • Change $('#user'). css({'border-color': '#A94442'}); by $( "#user" ).css( "border-color", "#A94442"); Reference: http://api.jquery.com/css/

  • got solution to that question? closes it.

1 answer

0


There’s a lot of things wrong with your code.

good come on, first use of this:

$(document).ready( function(){

can be replaced by this:

$(function () {

For it is the new and simplified form of jQuery.

Then you need to change it:

if($(usuario_existe == 1){
   $('#usuario').css({'border-color': '#A94442'});
}

because clearly there are errors, I did so simulating your php variable with a javascript, but it is only you assign the value:

usuario_existe = $('#usuario').val();

if(usuario_existe == 1){
   $('#usuario').css({'border-color': '#A94442'});
   alert('usuario_existe = 1'+usuario_existe);
}

I made this example in this jsfiddle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.