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
3 Answers
Reset to default 3I 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
版权声明:本文标题:Javascript: Loop through array while adding CSS styling - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745624274a2666874.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论