Igor Santana
Igor Santana

Reputation: 143

changing class with onkeypress

I'm learning javascript and i'm using bootstrap so i don't really have to worry about css. I have a form group and i want to add class as the user inputs something. Here's my code:

<div class="form-group" id="loginForm">
    <label for="inputLogin" class="pull-left">Login</label>
    <input type="text" class="form-control" id="inputLogin" placeholder="Enter login" onkeypress="changeToGreenBorder('loginForm','inputLogin')">
</div>

and it's calling those functions:

function changeToGreenBorder(string,string2){
        var tag = getElement(string);
        var tag2 = getElement(string2);
        if (tag2.innerHTML.length > 6) {
            tag.setAttribute('class','form-group has-success');
        } else if({
            tag.setAttribute('class','form-group has-error');
        }
    }

function getElement(string){
        var doc = document;
        return doc.getElementById(string);
    }

It's working with the error class, but when it has more then 6 letters, it doesn't switch to success class. How can i solve this?

Upvotes: 0

Views: 313

Answers (1)

MoshMage
MoshMage

Reputation: 506

You had a lingering ( and a Else If without a condition so I'm thinking you don't need a Else If You were also trying to get the lenght of the innerHTML when what you really wanted was the lengthof the elements' value;

Working JSFiddle

Upvotes: 1

Related Questions