uniformer
asked on
Custom Attribute Javascript Loop trough elements.
So i have this code:
I have multiple dropdowns, mu goal is to sum up the Custom Attribute "Price". But i cannot get the attribute to pull up the value. While example function "on" works. When i do it inside a loop it fails.
I tried:
Thanks
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
<body>
<form name="formtab1" id="formtab1">
<select name="ZoneDropdown" id="ZoneDropdown" onchange="DisplayFormValues()" class="dropdown">
<option value="" title="">Please Select</option>
<option value="BLP" title="POLOB" price="5.0000">BLP</option>
</select>
<select name="LogoDropdown" id="LogoDropdown" onchange="DisplayFormValues()" class="dropdown">
<option value="vv" title="tester" price="2.0000">Please Select</option>
<option value="BLP" title="POLO" price="5.0000">BACK</option>
</select>
<input id="AddEmb" name="AddEmb" type="text" />
</form>
<script type="text/javascript">
function DisplayFormValues() { var str = '';
var elem = document.getElementById('formtab1').elements;
for(var i = 0; i < elem.length; i++) { str += elem[i].value }; <----------- I need elem[i].price not value |
document.getElementById('AddEmb').value = str;
}
function on()
{
var w = document.formtab1.ZoneDropdown.selectedIndex;
var selected_text = document.formtab1.ZoneDropdown[document.formtab1.ZoneDropdown.selectedIndex].getAttribute('price');
alert(selected_text);
}
</script>
</body>
</html>
I have multiple dropdowns, mu goal is to sum up the Custom Attribute "Price". But i cannot get the attribute to pull up the value. While example function "on" works. When i do it inside a loop it fails.
I tried:
elem[i].price, elem[i].getAttribute('price')
and a bunch of things but nothing worked for me. Thanks
use : elem[i].getAttribute("pric e");
better(?) : parseFloat( elem[i].getAttribute("pric e") );
ASKER
[code]use : elem[i].getAttribute("pric e");[/code ]
I tried this in this
[code]for(var i = 0; i < elem.length; i++) { str += elem[i].value };[/code]
But the result not good. My whole goal in this loop is instead of
[code] elem[i].value [/code]
to get the Custom Atribute price.
I tried this in this
[code]for(var i = 0; i < elem.length; i++) { str += elem[i].value };[/code]
But the result not good. My whole goal in this loop is instead of
[code] elem[i].value [/code]
to get the Custom Atribute price.
You said : mu goal is to sum up the Custom Attribute "Price"
So :
So :
str = 0;
for(var i = 0; i < elem.length; i++) { str += elem[i].getAttribute("price"); };
Or better(?) :
str = 0;
for(var i = 0; i < elem.length; i++) { str += parseFloat(elem[i].getAttribute("price")); };
ASKER
[code]
function DisplayFormValues() { var str = '0';
var elem = document.getElementById('f ormtab1'). elements;
str = 0;
for(var i = 0; i < elem.length; i++) { str += parseFloat(elem[i].getAttr ibute("pri ce")); };
document.getElementById('A ddEmb').va lue = str;
}
[/code]
This code gives me "NaN" when testing.
[code]
function DisplayFormValues() { var str = '0';
var elem = document.getElementById('f ormtab1'). elements;
str = 0;
for(var i = 0; i < elem.length; i++) { str += elem[i].getAttribute("pric e"); };
document.getElementById('A ddEmb').va lue = str;
}
[/code]
This code gives me "0"
Am i doing something wrong?
Thanks
function DisplayFormValues() { var str = '0';
var elem = document.getElementById('f
str = 0;
for(var i = 0; i < elem.length; i++) { str += parseFloat(elem[i].getAttr
document.getElementById('A
}
[/code]
This code gives me "NaN" when testing.
[code]
function DisplayFormValues() { var str = '0';
var elem = document.getElementById('f
str = 0;
for(var i = 0; i < elem.length; i++) { str += elem[i].getAttribute("pric
document.getElementById('A
}
[/code]
This code gives me "0"
Am i doing something wrong?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yep! Thats it... I guess i was doing a few things wrong.. Thanks
You're welcome! Thanks for the points!