Reputation: 21
I have a little bit complicated html structure. I parse it to get this stuff:
Option1: value
Option2: value
from this like html:
<div class="option" id="option-691">
<span class="required">*</span>
<b>Option:</b><br>
<input type="radio" checked="" id="option-value-1250" value="1250" price="1156.0000" name="option[691]">
<label for="option-value-1250">2,35 xx - 1156.00 </label>
<br>
<input type="radio" onchange="recalculateprice();reloadpr();" id="option-value-1251" value="1251" price="506.0000" price_prefix="" points="0" name="option[691]">
<label for="option-value-1251">900 xx - 506.00</label>
<br>
</div>
and this too
<div class="option" id="option-690">
<span class="required">*</span>
<b>Option:</b><br>
<select name="option[690]">
<option price="1156.0000" price_prefix="+" points="0" value="1249">value01
(+1156.00)
</option>
<option price="1156.0000" price_prefix="+" points="0" value="1248">value02
(+1156.00)
</option>
<option price="1156.0000" price_prefix="+" points="0" value="1247">value03
(+1156.00)
</option>
</select>
</div>
And using this I can get data from both types (inputs + selects).
$('#product_options > div.option').each(function() {
var Opt01 = ( $(this).find('option:selected').parent().parent().find('b').text() + $(this).find('option:selected').text() );
var Opt02 = ( $(this).find('input:checked').parent().find('b').text() + $(this).find('input:checked').next().text() );
console.log(Opt01);
console.log(Opt02);
});
But, I want to get Opt01 and Opt02 outside the .each() loop. How I can do this?
Upvotes: 2
Views: 11244
Reputation: 21
Thanks guys. I've added another validation rule before loop and code looks like this, so far. And it works fine:
if ($('#product_options div.option').length) {
$('#product_options > div.option').each(function () {
//defining
OptionTitle = $(this).find('option:selected').parent().parent().find('b').text();
OptionValue = $(this).find('option:selected').text();
InputTitle = $(this).find('input:checked').parent().find('b').text();
InputValue = $(this).find('input:checked').next().text();
//
if (OptionTitle != '' && OptionValue != '') {
Opt01 = (OptionTitle + OptionValue)
}
if (InputTitle != '' && InputValue != '') {
Opt02 = (InputTitle + InputValue)
}
});
//get values outside the loop
console.log('Options detected');
if (typeof Opt01 === 'undefined') {
Opt01 = ''
}
if (typeof Opt02 === 'undefined') {
Opt02 = ''
}
console.log('Opt01 = ' + Opt01);
console.log('Opt02 = ' + Opt02);
}
else {
console.log('Options are empty');
var Opt01 = '';
var Opt02 = '';
console.log('Opt01 = ' + Opt01);
console.log('Opt02 = ' + Opt02);
}
Also, fiddle is here http://jsfiddle.net/caLj2xbe/
Upvotes: -1
Reputation: 14591
Just declare the variable outside the each loop. Variables declared inside the function can be accessed only from inside. JsFiddle
var Opt01, Opt02;
function find(){
$('#product_options > div.option').each(function() {
var option1 = ( $(this).find('option:selected').parent().parent().find('b').text() + $(this).find('option:selected').text() );
var option2 = ( $(this).find('input:checked').parent().find('b').text() + $(this).find('input:checked').next().text() );
if(option1){
Opt01 = option1;
console.log("inside: "+Opt01);
}
if(option2){
Opt02 = option2;
console.log("inside: "+Opt02);
}
});
console.log("outside: "+Opt01);
console.log("outside: "+Opt02);
}
Upvotes: 1
Reputation: 470
It's very simple,
You have to declare your variables outside of your function as global.
But don't declare only,assign some value
Var opt1=0;
Upvotes: -1
Reputation: 144689
I want to get Opt01 and Opt02 outside the .each() loop
Since the variables are local in that context (by using var keyword), outside of the each
callback the variables are undefined
. You could define them outside of the each callback, e.g. var opt1, opt2;
, but what will happen? In each iteration the values are overridden and the last values win. If the selector returns 1 element then each
shouldn't be used in the first place.
You could define a function and pass the values to it.
$('#product_options > div.option').each(function() {
var Opt1 = '...';
var Opt2 = '...';
doSomething(Opt1, Opt2);
});
Or use the map
method and create an array of objects:
var options = $('#product_options > div.option').map(function() {
var opt01 = '...';
var opt02 = '...';
return {
opt1: opt01,
opt2: opt02
}
}).get();
Upvotes: 1
Reputation: 435
Variables declared within functions can't be accessed outside of them. Just declare them outside of the loop:
var Opt01;
var Opt02;
$('#product_options > div.option').each(function() {
Opt01 = ( $(this).find('option:selected').parent().parent().find('b').text() + $(this).find('option:selected').text() );
Opt02 = ( $(this).find('input:checked').parent().find('b').text() + $(this).find('input:checked').next().text() );
console.log(Opt01);
console.log(Opt02);
});
Upvotes: 3