0

I created a form with a text field that has Spry Validation (ie javascript). The user can select the number of rows in the form from 1 to 10. I need the code below to also expand but I'm not familiar enough with javascript to make it work.

$divkey is the variable that controls how many rows are in the 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 I need the line 'var sprytextfield1...' to repeat based on $divkey with the next line being 'var sprytextfield2...' and so on. Can someone please rewrite this so it will work?

Trying to use php

<script type="text/javascript">
<?php   for ($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 to use javascript

<script type="text/javascript">
var numwrestler = <?php echo $wrestlerkey; ?>;
var sprytextfield = [];
for (var i = 0; i < 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>
2
  • In either attempt, what was the JavaScript which was emitted to the browser? Commented Jul 11, 2013 at 1:27
  • It looks to me like the only error in your JavaScript is the var in line beginning var sprytextfield[num] = ..., though you probably want to use index i, not num and it would be best practice to var num; outside of the loop and just set num inside (if you feel this variable necessary). Commented Jul 11, 2013 at 1:38

2 Answers 2

1

I'd recommend that you use a Javascript array for this type of task. Your code is mostly correct, but the var in your for loop is incorrect, and the creation of the num variable instead of just using i is redundant.

<script type="text/javascript">
var sprytextfield = new Array();
var numwrestler = <?php echo $wrestlerkey; ?>;
for(var i = 0; i < 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 that your PHP variable(s) are defined in the file before you include them in your script.

Sign up to request clarification or add additional context in comments.

3 Comments

This still gives sprytextfield form [undefined, Object, Object, ..]
very weird, anytime I make 'sprytextfield1 =' an array (ie sprytextfield[i] or sprytextfield[divnum] the javascript validation doesn't work. however this DOES work:
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield"+divnum, "none", {validateOn:["change"], maxChars:20}); ------- This is in the loop this way and the first sprytextfield1 doesn't mess it up... is that ok?
0

In your PHP code, you never define the variable divkey, by deafault the value will be 0. Try:

<script type="text/javascript">
<?php  $divkey = 10; for ($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>

Please, note that the variable that you are using as index $num in every iteration of the loop will be increasing 2, because of the i++ and the $num=$i+1

3 Comments

nope. $num is not increasing twice. It's getting $i+1 and $i is increasing once every iteration. So if $i=1 then $num=2, if $i=2, $num=3 etc :)
That's true, my fault. Sorry :\
$ divkey is defined further up the page. This not the complete page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.