admin管理员组

文章数量:1434949

I have a for loop.

In the loop there is an if statement that adds an error css style if the dropdown is found empty.

My problem is that the loop only loops 3 times then stops when it is supposed to loop 15 times.....and i do not know why.

The loop alone works fine, but but when i add the if statement, that's when it bees weird.

Help.

here is my loop

//add all the id's in an array. array size is 15
var drop_down=["Cars_chassis","Cars_model".....];

for (var i = 0; i < drop_down.length; i++) {
    //check if dropdown is empty
    if(document.getElementById(drop_down[i]).value == ""){

        //change the color of border 
        $('#'+drop_down[i]).css('border-color' , '#dddcdc');
     }
}

I have a for loop.

In the loop there is an if statement that adds an error css style if the dropdown is found empty.

My problem is that the loop only loops 3 times then stops when it is supposed to loop 15 times.....and i do not know why.

The loop alone works fine, but but when i add the if statement, that's when it bees weird.

Help.

here is my loop

//add all the id's in an array. array size is 15
var drop_down=["Cars_chassis","Cars_model".....];

for (var i = 0; i < drop_down.length; i++) {
    //check if dropdown is empty
    if(document.getElementById(drop_down[i]).value == ""){

        //change the color of border 
        $('#'+drop_down[i]).css('border-color' , '#dddcdc');
     }
}
Share Improve this question asked Mar 10, 2013 at 17:04 Victor NjorogeVictor Njoroge 3532 gold badges9 silver badges22 bronze badges 1
  • 1 Can you create a jsfiddle with your example? The loop doesn't seem to have anything odd in it that would cause it to break. – DanC Commented Mar 10, 2013 at 17:11
Add a ment  | 

3 Answers 3

Reset to default 3

I would suggest adding a CSS class to each one of these elements instead of specifying their id. Why manage all of those ids when one class can do the trick?

<select id="Cars_chassis" class="bordered-select"></select>
<!-- Add class to other 15 -->

At this point you could statically define a style for these drop downs in CSS.

.bordered-select{
   border-color: #DDDCDC;
}

Or set the style on the elements using the class selector. It appears your using jQuery so the following example would work.

$(".bordered-select").css('border-color', '#DDDCDC');

If you only need to highlight those without a value the following would remove those without a value from the matched set of elements:

$(".bordered-select").filter(function(){
  return $(this).val() == "";
}).css("border-color", "#DDDCDC");

Working Example: http://jsfiddle/v4hQz/

var drop_down=["Cars_chassis","Cars_model".....];

for (var i = 0; i < drop_down.length; i++) {
    //check if dropdown is empty
    if( $('#' + drop_down[i]).value == ""){

        //change the color of border 
        $('#'+drop_down[i]).css('border-color' , '#dddcdc');
     }
}

OR you can try

var drop_down=["Cars_chassis","Cars_model".....];
var contents = $('#'  + drop_down[i]);
for (var i = 0; i < drop_down.length; i++) {
    //check if dropdown is empty

    if( $(contents[0]).value == ""){

        //change the color of border 
        $('#'+drop_down[i]).css('border-color' , '#dddcdc');
     }
}
document.getElementById(drop_down[i]); //returns a HTML DOM Object

var contents = $('#' + drop_down[i]);  //returns a jQuery Object

You can try replace the if (javascript) to if (jquery syntax)

本文标签: Javascript Loop through array while adding CSS stylingStack Overflow