www.webdeveloper.com
Results 1 to 3 of 3

Thread: need help simplifying javascript with loop

  1. #1
    Join Date
    Jun 2010
    Posts
    2

    need help simplifying javascript with loop

    I have a piece of javascript that works exactly how I want it, but now I need to make it less unweildy.

    Essentially, I have 25 copies of this code, with the "testform1" increasing to "testform2," testform3," ... "testform25." In addition, each of the sections echoes php code "name_product_1," "name_product_2," ... "name_product_13."

    Certainly a loop would fix the first problem, if not both? I know how to loop a document.write method, but I don't know how to loop within the javascript itself.

    Code:
    <script language="JavaScript">
    function textValue() {   
    var searchInteger, searchString       
    testInteger=document.testform1.testselect.selectedIndex 
    testString=document.testform1.testselect.options[testInteger].text
    if (testInteger=document.testform1.testselect.options[0].selected) { 
    document.testform1.testtext.value = "No package selected";}
    else if (testInteger=document.testform1.testselect.options[1].selected) { 
    document.testform1.testtext.value = "<?php echo $orders_row->getField('name_product_1',0); ?>";}
    else if (testInteger=document.testform1.testselect.options[2].selected) { 
    document.testform1.testtext.value = "<?php echo $orders_row->getField('name_product_2',0); ?>";}
    else if (testInteger=document.testform1.testselect.options[3].selected) { 
    document.testform1.testtext.value = "<?php echo $orders_row->getField('name_product_3',0); ?>";}
    else if (testInteger=document.testform1.testselect.options[4].selected) { 
    document.testform1.testtext.value = "<?php echo $orders_row->getField('name_product_4',0); ?>";}
    else if (testInteger=document.testform1.testselect.options[5].selected) { 
    document.testform1.testtext.value = "<?php echo $orders_row->getField('name_product_5',0); ?>";}
    else if (testInteger=document.testform1.testselect.options[6].selected) { 
    document.testform1.testtext.value = "<?php echo $orders_row->getField('name_product_6',0); ?>";}
    else if (testInteger=document.testform1.testselect.options[7].selected) { 
    document.testform1.testtext.value = "<?php echo $orders_row->getField('name_product_7',0); ?>";}
    else if (testInteger=document.testform1.testselect.options[8].selected) { 
    document.testform1.testtext.value = "<?php echo $orders_row->getField('name_product_8',0); ?>";}
    else if (testInteger=document.testform1.testselect.options[9].selected) { 
    document.testform1.testtext.value = "<?php echo $orders_row->getField('name_product_9',0); ?>";}
    else if (testInteger=document.testform1.testselect.options[10].selected) { 
    document.testform1.testtext.value = "<?php echo $orders_row->getField('name_product_10',0); ?>";}
    else if (testInteger=document.testform1.testselect.options[11].selected) { 
    document.testform1.testtext.value = "<?php echo $orders_row->getField('name_product_11',0); ?>";}
    else if (testInteger=document.testform1.testselect.options[12].selected) { 
    document.testform1.testtext.value = "<?php echo $orders_row->getField('name_product_12',0); ?>";}
    else if (testInteger=document.testform1.testselect.options[13].selected) { 
    document.testform1.testtext.value = "<?php echo $orders_row->getField('name_product_13',0); ?>";}
    }
    </script>
    
    
    <script language="JavaScript">
    function textValue() {   
    var searchInteger, searchString       
    testInteger=document.testform2.testselect.selectedIndex 
    testString=document.testform2.testselect.options[testInteger].text
    if (testInteger=document.testform2.testselect.options[0].selected) { 
    document.testform2.testtext.value = "No package selected";}
    else if (testInteger=document.testform2.testselect.options[1].selected) { 
    document.testform2.testtext.value = "<?php echo $orders_row->getField('name_product_1',0); ?>";}
    else if (testInteger=document.testform2.testselect.options[2].selected) { 
    document.testform2.testtext.value = "<?php echo $orders_row->getField('name_product_2',0); ?>";}
    else if (testInteger=document.testform2.testselect.options[3].selected) { 
    document.testform2.testtext.value = "<?php echo $orders_row->getField('name_product_3',0); ?>";}
    else if (testInteger=document.testform2.testselect.options[4].selected) { 
    document.testform2.testtext.value = "<?php echo $orders_row->getField('name_product_4',0); ?>";}
    else if (testInteger=document.testform2.testselect.options[5].selected) { 
    document.testform2.testtext.value = "<?php echo $orders_row->getField('name_product_5',0); ?>";}
    else if (testInteger=document.testform2.testselect.options[6].selected) { 
    document.testform2.testtext.value = "<?php echo $orders_row->getField('name_product_6',0); ?>";}
    else if (testInteger=document.testform2.testselect.options[7].selected) { 
    document.testform2.testtext.value = "<?php echo $orders_row->getField('name_product_7',0); ?>";}
    else if (testInteger=document.testform2.testselect.options[8].selected) { 
    document.testform2.testtext.value = "<?php echo $orders_row->getField('name_product_8',0); ?>";}
    else if (testInteger=document.testform2.testselect.options[9].selected) { 
    document.testform2.testtext.value = "<?php echo $orders_row->getField('name_product_9',0); ?>";}
    else if (testInteger=document.testform2.testselect.options[10].selected) { 
    document.testform2.testtext.value = "<?php echo $orders_row->getField('name_product_10',0); ?>";}
    else if (testInteger=document.testform2.testselect.options[11].selected) { 
    document.testform2.testtext.value = "<?php echo $orders_row->getField('name_product_11',0); ?>";}
    else if (testInteger=document.testform2.testselect.options[12].selected) { 
    document.testform2.testtext.value = "<?php echo $orders_row->getField('name_product_12',0); ?>";}
    else if (testInteger=document.testform2.testselect.options[13].selected) { 
    document.testform2.testtext.value = "<?php echo $orders_row->getField('name_product_13',0); ?>";}
    }
    </script>

  2. #2
    Join Date
    Feb 2009
    Location
    UK
    Posts
    142
    would making "document.testform2.testtext.value" an object in its own right simplify things?
    it would shorten the code, not sure it would run quicker but easier to debug.

  3. #3
    Join Date
    Aug 2007
    Posts
    3,767
    You can't. You have to write the loop on the PHP side, which will echo out the full code. Unpleasant, but necessary.
    PHP Code:
    for ($i 1$i 26$i++) {
        
    //whatever
        
    echo 'javascript;'


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles