Skip to main content
Participating Frequently
December 24, 2010
Question

cfloop breaks javascript

  • December 24, 2010
  • 2 replies
  • 777 views

I've been using a spry validation text field to check that the user is entering a numeric character, I've used it on other pages to loop through a series of checkboxes that are populated using a cfquery but on one page I need to cfloop through an array but the first part of the cfloop is breaking the javascript, the code is below, any suggestions would be appreciated or is there another way of achieving the same result.

<cfloop from="1" to="#ArrayLen(session.cart)#" index="i">

<script type="text/javascript">

<!--

var sprytextfield#session.cart.productID# = new Spry.Widget.ValidationTextField("sprytextfield#session.cart.productID#", "integer", {validateOn:["change"], minValue:1});

//-->

</script>

</cfloop>

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    December 24, 2010

    Firstly, suppose the array's length is 100. Then your code will generate 100 Javascript scripts. Is that what you want?

    Secondly, I miss the cfoutput tag. If, as I suspect, you only wish to generate the variables dynamically in one script, then you should be doing something like

    <script type="text/javascript">

    <!--

    <cfloop from="1" to="#ArrayLen(session.cart)#" index="i">

    <cfoutput>var  sprytextfield#session.cart.productID# = new  Spry.Widget.ValidationTextField("sprytextfield#session.cart.produc  tID#", "integer", {validateOn:["change"], minValue:1});</cfoutput>

    </cfloop>

    //-->

    </script>

    Inspiring
    December 24, 2010
    [...] is breaking the javascript, the code is below, any suggestions would be appreciated

    Sure.  Describing in what way it's breaking the JS would be helpful.

    --

    Adam

    pwizzardAuthor
    Participating Frequently
    December 24, 2010

    The field is for the quantity of a particular product, so only numeric characters are required, so entering any alpha characters displays an error when a calculation is performed.

    dave_jf
    Participating Frequently
    December 24, 2010

    So, first off how would an alpha get into a quantity field? You should be

    using a drop down with only numerics in it. Alternatively, you should put

    a validator on the field itself so that on focus change you can validate

    that the field only has a numeric.

    I think you are trying to fix the issue in the wrong place.

    Also, in your code, you should alter where the script tags are. You don't

    need to generate a script tag for every loop. Also, I assume there is a

    cfoutput surrounding the loop right?

    var sprytextfield#session.cart.productID# = new

    Spry.Widget.ValidationTextField("sprytextfield#session.cart.produc

    tID#", "integer", );

    </cfloop>

    //-->

    </script>

    </cfoutput>

    --Dave