Problem with Functions!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thanawala27
    New Member
    • Mar 2007
    • 68

    Problem with Functions!

    hi

    I'm facing a small issue regarding the function

    now what i want to do is, call a function without using Onchange or onclick or any of these options.

    this is a simple code i have written: jus to get some idea

    -------------------------------------------
    [code=html]<html>
    <script language=javasc ript>
    function diableitem()
    {
    document.try.te xt.style.backgr oundColor = "#eeeeee";

    }
    </script>

    <body>
    <form name=try>

    <input type=text name=num value=100>

    disableitem()

    </form>
    </body>
    </html>[/code]

    -------------------------------------------------

    i want to call this disableitem function.

    im not able to do it here, as it jus prints "disableite m()" on the browser, doesnt call it.

    Any help is appreciated.

    Thank You.
    Last edited by pbmods; Jul 24 '07, 07:14 PM. Reason: Added CODE tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, thanawala27.

    Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

    You're getting this error because document.try.te xt does not exist. Give your INPUT an id property, and then use document.getEle mentById() instead.

    Comment

    • epots9
      Recognized Expert Top Contributor
      • May 2007
      • 1352

      #3
      do what pbmods said and also call the function from the body tag, using the 'onload' attribute.

      Comment

      • thanawala27
        New Member
        • Mar 2007
        • 68

        #4
        HI,

        I TRIED WHAT YOU MENTIONED, BUT STILL THE FUNCTION IS NOT CALLED

        THIS IS THE CODE I TRIED

        [CODE=html]

        <html>
        <script language=javasc ript>
        function diableitem()
        {
        element = document.getEle mentById(num);

        element.text.st yle.backgroundC olor = "#eeeeee";

        }
        </script>

        <body>
        <form name=try>

        <input type=text name=num value=100>

        disableitem()

        </form>
        </body>
        </html>


        [/CODE]


        I JUS WANT TO CALL THIS FUNCTION, WITHOUT USING ONCHANGE, OR ONCLICK.BUT IM FACING PROBLEMS WITH IT.

        THANKS FOR ALL THE REPLIES
        Last edited by gits; Jul 25 '07, 06:17 AM. Reason: fix CODE tags

        Comment

        • thanawala27
          New Member
          • Mar 2007
          • 68

          #5
          @epots9

          I DON'T WANT TO CALL WHEN THE BODY IS LOADED. AS THERE IS SOME PROCESSING DONE, BEFORE THIS FUNCTION IS CALLED.

          THANKS

          Comment

          • epots9
            Recognized Expert Top Contributor
            • May 2007
            • 1352

            #6
            Originally posted by thanawala27
            HI,

            I TRIED WHAT YOU MENTIONED, BUT STILL THE FUNCTION IS NOT CALLED

            THIS IS THE CODE I TRIED

            [html]

            <html>
            <script language=javasc ript>
            function diableitem()
            {
            element = document.getEle mentById(num);

            element.text.st yle.backgroundC olor = "#eeeeee";

            }
            </script>

            <body>
            <form name=try>

            <input type=text name=num value=100>

            disableitem()

            </form>
            </body>
            </html>


            [/html]


            I JUS WANT TO CALL THIS FUNCTION, WITHOUT USING ONCHANGE, OR ONCLICK.BUT IM FACING PROBLEMS WITH IT.

            THANKS FOR ALL THE REPLIES
            give this a try:

            [code=javascript]
            function diableitem()
            {
            document.getEle mentById(num).s tyle.background Color = "#eeeeee";
            }
            [/code]
            and
            [html]
            <body onload="diablei tem();">
            [/html]

            give that a shot, but u do know that won't disable anything...just change the background color.

            good luck

            Comment

            • thanawala27
              New Member
              • Mar 2007
              • 68

              #7
              GOT IT!!!

              to call a function without using ONCHANGE OR ONCLICK, following needs to be done
              CALL THE FUNCTION BETWEEN <script> tags as shown below:


              [CODE=html]

              <html>
              <script language=javasc ript>
              function disableitem()
              {
              alert ("VERY HAPPY!!!")
              }
              </script>

              <body>
              <form name=try>

              <input type=text name=num value=100>

              <script type="text/javascript">
              disableitem();
              </script>

              </form>
              </body>
              </html>

              [/CODE]

              IT WORKS FINE.

              THANKS FOR ALL THE INPUTS FOLKS
              Last edited by gits; Jul 25 '07, 06:18 AM. Reason: fix CODE tags again

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                hi ...

                yes ... that works of course ... but when you use document.getEle mentById(); in your function i may (probably will) give you an error ... since the document dom is reliably ready in onload of the page ... so you cannot rely on the execution during load ... try it ;)

                kind regards

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  thanawala27, please do not type in all-caps. Thanks!

                  Comment

                  Working...