I have a password strength bar which which uses li items which are styled with different colours to indicate to the user how strong their password is, like so:
<input type="password" name="password" id="password" placeholder="Password">
<ul id="passwordStrength">
<li class="point-reg"></li>
<li class="point-reg"></li>
<li class="point-reg"></li>
<li class="point-reg"></li>
</ul>
When a regEx criteria has been met through a set of if statements, the counter is incremented and a switch statement then adds the necessary CSS background property to each li item depending on the counter value.
var password_li = $('#passwordStrength').find('li');
var counter = 0;
if(pw.match(/[A-Z]/) && pw.match(/[a-z]/)) {
counter++;
}
//further regex if statements
switch(counter) { //patrick
case 0:
break;
case 1:
$(password_li[0]).css('background', '#e30613'); //first li
break;
case 2:
$(password_li[0]).css('background', '#e30613'); //first li
$(password_li[1]).css('background', '#f9b233'); //second li
break;
case 3:
$(password_li[0]).css('background', '#e30613'); //first li
$(password_li[1]).css('background', '#f9b233'); //second li
$(password_li[2]).css('background', '#53ab58'); //third li
break;
case 4:
$(password_li[0]).css('background', '#e30613'); //first li
$(password_li[1]).css('background', '#f9b233'); //second li
$(password_li[2]).css('background', '#53ab58'); //third li
$(password_li[3]).css('background', '#418746'); //fourth li
break;
}
This doesn't seem the most efficient way of styling the li items. Also, if the user then deletes the value they have entered, the li items remain the colour they have been assigned, rather than reverting back to their default colour of grey, which makes sense as there isn't a function in place to remove the styling. I can add grey to the li items depending on what case has been met in the switch statement like so:
case 1:
$(password_li[0]).css('background', '#e30613');
$(password_li[1]).css('background', '#dcdcdc');
$(password_li[2]).css('background', '#dcdcdc');
$(password_li[3]).css('background', '#dcdcdc');
break;
But again, this does not seem the most efficient apporach.
Question
How do I approach this functionality so the styling for the li elements can be dynamically added and removed?
Here is a JSFiddle
data-attribute and then read them in CSS viaattr(), but unfortunately I don't think any browser currently supports that :(contentin::beforeand::after).