I'm trying to decrease or increase the value in a textbox using javascript. I made it work, when not using any arrays on it. If you're asking why I need arrays on this one. Its because I'm gonna need to integrate it later on a bigger project which heavily needs array on it. I'm a beginner in javascript, so I don't really know what are the requirements in making this work when making use of arrays.
<html>
<head>
<script type="text/javascript">
function blabla(){
var a= document.x.qty[].value;
var b=document.x.qty2[].value;
var pa=parseInt(a);
var plusqty= pa + 1;
var txt = plusqty;
var tbox = document.getElementById('qty');
if (tbox)
{
tbox.value = txt;
}
}
function lastog(){
var a= document.x.qty.value;
var b=document.x.qty2.value;
var pa=parseInt(a);
var plusqty= pa - 1;
var txt = plusqty;
var tbox = document.getElementById('qty');
if (tbox)
{
tbox.value = txt;
}
}
</script>
</head>
<body>
<form name="x">
number 1<input type="text" id="qty" name="qty[]" value=""><br/>
number 2<input type="text" id="qty2" name="qty2" value=""><br/>
number 3<input type="text" id="qty3" name="qty3" value=""><br/>
<a href=""><img src="add-icon.png" onmouseover="blabla();"></img></a>
<a href=""><img src="delete-icon.png" onmouseover="lastog();"></img></a>
</form>
</body>
</html>
I also tried iterating through it using the usual for loop, but I cant make it work:
function blabla(){
for (int i=0; i<arr.length; i++){
var a= document.x.qty[].value;
var b=document.x.qty2[].value;
var pa=parseInt(a);
var plusqty= pa + 1;
var txt = plusqty;
var tbox = document.getElementById('qty');
if (tbox)
{
tbox.value = txt;
}
}
}
document.getElementById('qty')instead ofdocument.x.qty[], etc. for starters.<label>element.<br/>isn't semantic; you should use CSS if you want each input on a separate line. Read "Fancy Form Design", "Label Placement & 1ayr vs. 2ary Actions", "Web App. Form Design" for more.