Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Javascript for loop

Hello,

I have javascript code ready for a grid type of question. In a grid I have 7 rows and I would like to have "for" loop so I wouldn`t have to copy my code 7 times for each row. This is the code:

<script type="text/javascript">

function vnos()
{
if(document.getElementById("v3_r1_c1").value.length>=1)
{
SSI_SetSelect("v3_r1_c2", false); 
}
}
document.getElementById("v3_r1_c1").onkeydown = vnos;


</script>


And this is my try:
script type="text/javascript">

function vnos()
{
var i=1;
for (i=1; i<7; i++)
{
if(document.getElementById("v3_r"+i+"_c1").value.length>=1)
{
SSI_SetSelect("v3_r"+i+"_c2", false); 
}
}
document.getElementById("v3_r"+i+"_c1").onkeydown = vnos;
}

</script>
asked Dec 18, 2013 by Miha (175 points)

1 Answer

0 votes
document.getElementById("v3_r"+i+"_c1").onkeydown = vnos;

^ probably should be outside of the vnos function and in it's own loop, also in the loop you may want "i<=7;"
answered Dec 27, 2013 by anonymous
Ok. Because I am not javascript expert (only familiar with it) I must go further with my question, I tried with your tip and use another function but was not successful.  My code look like this with functions from vnos1 to vnos6 and I don`t know hot to put it in for loop.
<script type="text/javascript">
 
function vnos1()
{
if(document.getElementById("v3_r1_c1").value.length>=1)
{
SSI_SetSelect("v3_r1_c2", false);
}
}
document.getElementById("v3_r1_c1").onkeydown = vnos1;
 
 
</script>

<script type="text/javascript">
function vnos2()
{
if(document.getElementById("v3_r2_c1").value.length>=1)
{
SSI_SetSelect("v3_r2_c2", false);
}
}
document.getElementById("v3_r2_c1").onkeydown = vnos2;
 
 
</script>
...