Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Repetive call to script

    I have about 30 inputs on my page using this in my form page and was wondering how I can call the same script part that is below in a function or a more efficient way of doing it so I dont have to put the <script>...</script> in my form 30 times.

    Code:
    <form action="goto.cfm" method="post">
    <input name="other" type="text">
    <input name="otherTwo" type="text">
    <select name="stateIssue">
        <script language="javascript">
        for (var i=1; i<101;i++) {
            document.write ("<option value='"+i+"'>"+i+"</option>")
        }
        </script>
    </select>
    <input name="otherA" type="text">
    <input name="otherTwoB" type="text">
    <select name="mynameCity">
        <script language="javascript">
        for (var i=1; i<101;i++) {
            document.write ("<option value='"+i+"'>"+i+"</option>")
        }
        </script>
    </select>
    <input name="other" type="text">
    <input name="otherTwo" type="text">
    ...
    </form>

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Code:
    <script type="text/javascript">
      function outputText(){
        document.write("<p>hi</p>");
      }
    </script>
    
    <script type="text/javascript">
      outputText();
    </script>
    You could also do alot of other things, but this is the simpliest

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks,

    I would still have to put the call to this 30 times in my form page.
    Code:
    <script type="text/javascript">
      outputText();
    </script>
    Is that efficient to have that much <script> and </script> in my form page?

    Or is there a more efficient way for me to do this besides putting in this in there instead 30 times which would not have the script tags but would take up more html code:
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    ...100 times...

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    More efficient way would be not to use JavaScript at all to build up all of the elements and have a serverside language do it!

    If you want to wait until after the page load you can do things like have an array of elements and add values. Meaning you would not have to add it multiple times.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for all the info!

    I will do this server side instead of client side as you suggested.


  •  

    Posting Permissions

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