how do I loop a php variable within a javascript script -


i created form text field has spry validation (ie javascript). user can select number of rows in form 1 10. need code below expand i'm not familiar enough javascript make work.

$divkey variable controls how many rows in form.

original

<script type="text/javascript"> var sprytextfield1 = new spry.widget.validationtextfield("sprytextfield1", "none", {validateon:["change"], maxchars:20}); var sprytooltip1 = new spry.widget.tooltip("sprytooltip1", "#sprytrigger1"); </script> 

so need line 'var sprytextfield1...' repeat based on $divkey next line being 'var sprytextfield2...' , on. can please rewrite work?

trying use php

<script type="text/javascript"> <?php   ($i = 0; $i < $divkey; $i++) { $num=$i+1; ?>   var sprytextfield<?php echo $num;?> = new spry.widget.validationtextfield("sprytextfield<?php echo $num;?>", "none", {validateon:["change"], maxchars:20}); <?php }?> var sprytooltip1 = new spry.widget.tooltip("sprytooltip1", "#sprytrigger1"); </script> 

trying use javascript

<script type="text/javascript"> var numwrestler = <?php echo $wrestlerkey; ?>; var sprytextfield = []; (var = 0; < numwrestler; i++) {     var num = i+1;   var sprytextfield[num] = new spry.widget.validationtextfield("sprytextfield"+num, "none", {validateon:["change"], maxchars:20}); } var sprytooltip1 = new spry.widget.tooltip("sprytooltip1", "#sprytrigger1"); </script> 

i'd recommend use javascript array type of task. code correct, var in loop incorrect, , creation of num variable instead of using i redundant.

<script type="text/javascript"> var sprytextfield = new array(); var numwrestler = <?php echo $wrestlerkey; ?>; for(var = 0; < numwrestler; i++){     sprytextfield[i] = new spry.widget.validationtextfield("sprytextfield"+(i+1), "none", {validateon:["change"], maxchars:20}); }  var sprytooltip1 = new spry.widget.tooltip("sprytooltip1", "#sprytrigger1"); </script> 

be sure php variable(s) defined in file before include them in script.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -