Uncaught SyntaxError: Illegal break statement
This is the warning i got when i try to break of the loop. What I want to do break of the loop when the condition is meet.In the example code I have two active class where I want to break out of the loop completely when the first active class is encountered.
<ul>
<li id="foo1" class="bar">1</li>
<li id="foo2" class="bar">2</li>
<li id="foo3" class="bar">3</li>
<li id="foo4" class="bar">4</li>
<li id="foo5" class="active">5</li>
<li id="foo6" class="bar">6</li>
<li id="foo7" class="bar">7</li>
<li id="foo8" class="active">8</li>
</ul>
js
$(function(){
function setup(){
var x,y,z;
$("ul li").each(function(){
//console.log($(this)[0].className);
if($(this)[0].className === 'active'){
x = $(this)[0].className;
y = $(this)[0].tagName;
z = $(this)[0].id;
break;
}
});
var return_values = {
class : x,
tag : y,
id : z
}
return(return_values);
}
var data = setup();
console.log(data.class,data.tag,data.id);
});
JSFIDDLE here