Passing objects into javascript functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Evan

    Passing objects into javascript functions

    I have a fairly simple problem that I just cant seem to figure out. I
    am trying to pass and use a div in a function. This is what I have so
    far... it doesnt work though...

    <script>
    function divControl(divN ame){
    divName.innerHT ML="test"
    }
    </script>

    in the HTML I have three div's

    <div id="test1"></div>
    <div id="test2"></div>
    <a onclick="divCon trol('test1')"> test3</a>

    The system does not seem to recognize the divName in the function ?!?!

    Any help would be greatly appreciated

    Evan
  • Randy Webb

    #2
    Re: Passing objects into javascript functions

    Evan wrote:
    [color=blue]
    > I have a fairly simple problem that I just cant seem to figure out. I
    > am trying to pass and use a div in a function. This is what I have so
    > far... it doesnt work though...
    >
    > <script>
    > function divControl(divN ame){
    > divName.innerHT ML="test"
    > }
    > </script>
    >
    > in the HTML I have three div's
    >
    > <div id="test1"></div>
    > <div id="test2"></div>
    > <a onclick="divCon trol('test1')"> test3</a>
    >
    > The system does not seem to recognize the divName in the function ?!?!
    >
    > Any help would be greatly appreciated
    >
    > Evan[/color]




    Addresses that issue directly.

    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq
    Answer:It destroys the order of the conversation
    Question: Why?
    Answer: Top-Posting.
    Question: Whats the most annoying thing on Usenet?

    Comment

    • Lee

      #3
      Re: Passing objects into javascript functions

      Evan said:[color=blue]
      >
      >I have a fairly simple problem that I just cant seem to figure out. I
      >am trying to pass and use a div in a function. This is what I have so
      >far... it doesnt work though...
      >
      ><script>
      > function divControl(divN ame){
      > divName.innerHT ML="test"
      > }
      ></script>
      >
      >in the HTML I have three div's
      >
      ><div id="test1"></div>
      ><div id="test2"></div>
      ><a onclick="divCon trol('test1')"> test3</a>
      >
      >The system does not seem to recognize the divName in the function ?!?!
      >
      >Any help would be greatly appreciated[/color]


      You're not passing an object. You're passing a string
      containing the id of an object. That string does not
      have an innerHTML attribute.

      You can either pass a reference to the object, or you can
      try to obtain a reference to the object in the function:

      <script type="text/javascript">
      function divControl(divN ame){
      var divRef=document .getElementById (divName);
      divRef.innerHTM L="test"
      }
      </script>


      In a production script, there should be error checking, too.

      Comment

      • Fred Oz

        #4
        Re: Passing objects into javascript functions

        Lee wrote:[color=blue]
        > Evan said:
        >[color=green]
        >>I have a fairly simple problem that I just cant seem to figure out. I
        >>am trying to pass and use a div in a function. This is what I have so
        >>far... it doesnt work though...[/color][/color]

        Another method is to pass the object reference, rather than just a
        string. This can be simpler in some cases, the OP's script stays the
        same but the HTML changes to:

        ...
        <a onclick="divCon trol(document.g etElementById(' test1'))">test3 </a>

        The difference here is that you are not bound to use getElementById, so
        you can use any method (this, this.parentNode , this.form, etc.) to get
        the reference and pass it to the function.

        Cheers, Fred.

        Comment

        • Raul

          #5
          Re: Passing objects into javascript functions

          evandelagrammat icas@hotmail.co m (Evan) wrote in message news:<53203c78. 0410161614.3598 d0ad@posting.go ogle.com>...[color=blue]
          > I have a fairly simple problem that I just cant seem to figure out. I
          > am trying to pass and use a div in a function. This is what I have so
          > far... it doesnt work though...
          >
          > <script>
          > function divControl(divN ame){
          > divName.innerHT ML="test"
          > }
          > </script>
          >
          > in the HTML I have three div's
          >
          > <div id="test1"></div>
          > <div id="test2"></div>
          > <a onclick="divCon trol('test1')"> test3</a>
          >
          > The system does not seem to recognize the divName in the function ?!?!
          >
          > Any help would be greatly appreciated
          >
          > Evan[/color]

          Try this:
          <script>
          function divControl(divN ame){
          divName.innerHT ML="test"
          }
          </script>
          </HEAD>
          <div id=test1></div>
          <div id=test2></div>
          <a href='' onclick="divCon trol(test1);ret urn false">test3</a>

          Cheers
          Raul

          Comment

          • Michael Winter

            #6
            Re: Passing objects into javascript functions

            On 17 Oct 2004 00:48:50 -0700, Raul <ddanone@arraki s.es> wrote:

            [snip]
            [color=blue]
            > Try this:[/color]

            Please don't.
            [color=blue]
            > <script>[/color]

            SCRIPT elements have a required type attribute.

            <script type="text/javascript">
            [color=blue]
            > function divControl(divN ame){
            > divName.innerHT ML="test"
            > }
            > </script>
            > </HEAD>
            > <div id=test1></div>
            > <div id=test2></div>
            > <a href='' onclick="divCon trol(test1);ret urn false">test3</a>[/color]

            You still aren't passing an object reference, just an undefined global
            variable. That will do nothing but cause an error. I suggest you read the
            FAQ (<URL:http://jibbering.com/faq/>), specifically section 4.41.

            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail.

            Comment

            • shailesh

              #7
              Re: Passing objects into javascript functions

              There is no onClick for "<a>" anchor Tag's and the innerHTML will embed
              HTML not Text.Check Out New Code it Should Work.

              <SCRIPT LANGUAGE="JavaS cript">
              <!--
              //Javascript for Div Tag Creation.
              function divControl(divN ame,divName1){
              divName.innerTe xt="test";
              divName1.innerT ext = "Deepak Kini";
              }
              //-->
              </SCRIPT>
              </HEAD>
              <BODY>
              <!--Insert Text into Div Tags-->
              <div id="test1"></div>
              <a href="javascrip t:divControl(te st1,test2)">tes t3</a>
              <div id="test2"></div>
              <!-- End -->


              Comment

              • Michael Winter

                #8
                Re: Passing objects into javascript functions

                On Sun, 17 Oct 2004 08:03:30 -0400, shailesh <shailesh1@vasn et.co.in>
                wrote:
                [color=blue]
                > There is no onClick for "<a>" anchor Tag's[/color]

                Nonsense. Of course there is!
                [color=blue]
                > and the innerHTML will embed HTML not Text.[/color]

                Wrong. If you assign only text, only text will be displayed (assuming
                innerHTML is supported).
                [color=blue]
                > Check Out New Code it Should Work.[/color]

                In IE, but few others. Better solutions have already been suggested.
                [color=blue]
                > <SCRIPT LANGUAGE="JavaS cript">[/color]

                The language attribute is deprecated in favour of (required) type.

                <script type="text/javascript">
                [color=blue]
                > <!--[/color]

                Attempting to hide scripts is also an out-dated practice. All user agents
                currently in use know what a SCRIPT element is, so they will not render
                its contents even if they cannot execute the script.
                [color=blue]
                > //Javascript for Div Tag Creation.
                > function divControl(divN ame,divName1){
                > divName.innerTe xt="test";
                > divName1.innerT ext = "Deepak Kini";[/color]

                The innerText property is a proprietary Microsoft mechanism that is not
                supported very well by other browsers. Unless you're writing for IE only,
                and the OP gave no indication of that, don't use it.
                [color=blue]
                > }
                > //-->
                > </SCRIPT>
                > </HEAD>
                > <BODY>
                > <!--Insert Text into Div Tags-->
                > <div id="test1"></div>
                > <a href="javascrip t:divControl(te st1,test2)">tes t3</a>[/color]

                You need to read the group FAQ (<URL:http://jibbering.com/faq/>).
                [color=blue]
                > <div id="test2"></div>
                > <!-- End -->[/color]

                Mike

                --
                Michael Winter
                Replace ".invalid" with ".uk" to reply by e-mail.

                Comment

                Working...