Javascript parameters

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

    Javascript parameters

    Hi,

    I have a Javascript function called changediv() that receives as a parameter
    the
    number that is the id of the element I want to change:

    function changediv(divnu m) {

    }

    On the web page I have the following code:

    <div id="div1" onclick="change div(1)">...</div>

    This works OK.
    But if I try to use the more modern way of associating an event with
    an event handler, like,

    function init() {
    document.getEle mentById('div1' ).onclick=chang ediv(1);
    }
    window.onload=i nit;

    it doesn't work because of the parameter. How can I pass a parameter
    in this situation?

    TIA

    Paulo Almeida


  • Dan Rumney

    #2
    Re: Javascript parameters

    Paulo Almeida wrote:
    Hi,
    >
    I have a Javascript function called changediv() that receives as a parameter
    the
    number that is the id of the element I want to change:
    >
    function changediv(divnu m) {
    >
    }
    >
    On the web page I have the following code:
    >
    <div id="div1" onclick="change div(1)">...</div>
    >
    This works OK.
    But if I try to use the more modern way of associating an event with
    an event handler, like,
    >
    function init() {
    document.getEle mentById('div1' ).onclick=chang ediv(1);
    }
    window.onload=i nit;
    >
    it doesn't work because of the parameter. How can I pass a parameter
    in this situation?
    The onclick property of an element expects you to pass it a function.

    In the code that you've provided, you've passed it whatever the call to
    changediv(1) returns. (I'm guessing null or undefined).

    What you want to use is:

    function init() {
    document.getEle mentById('div1' ).onclick=funct ion(){changediv (1);};
    }

    Comment

    • Jonathan N. Little

      #3
      Re: Javascript parameters

      Paulo Almeida wrote:
      Hi,
      >
      I have a Javascript function called changediv() that receives as a parameter
      the
      number that is the id of the element I want to change:
      >
      function changediv(divnu m) {
      >
      }
      >
      On the web page I have the following code:
      >
      <div id="div1" onclick="change div(1)">...</div>
      >
      This works OK.
      But if I try to use the more modern way of associating an event with
      an event handler, like,
      >
      function init() {
      document.getEle mentById('div1' ).onclick=chang ediv(1);
      }
      window.onload=i nit;
      >
      it doesn't work because of the parameter. How can I pass a parameter
      in this situation?
      Don't, just self reference with 'this'

      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">
      <html>
      <head>
      <title>Exampl e</title>

      <style type="text/css">
      div { margin: 1em; padding: 1em; background: #eee; }
      </style>

      <script type="text/javascript">
      function changediv() {
      var me=this;
      alert("My id is " + me.id);
      }

      function init(){
      var target=document .getElementById ('div1');
      target.onclick= changediv;
      target=document .getElementById ('div2');
      target.onclick= changediv;
      }

      window.onload=i nit;

      </script>

      </head>
      <body>

      <div id="div1">Clic k the target DIV1</div>
      <div id="div2">Clic k this other DIV2</div>

      </body>
      </html>

      --
      Take care,

      Jonathan
      -------------------
      LITTLE WORKS STUDIO

      Comment

      • Paulo Almeida

        #4
        Re: Javascript parameters

        Thank you guys.

        Paulo Almeida

        "Paulo Almeida" <teste@teste.pt wrote in message
        news:484bfaea$0 $27015$a729d347 @news.telepac.p t...
        Hi,
        >
        I have a Javascript function called changediv() that receives as a
        parameter the
        number that is the id of the element I want to change:
        >
        function changediv(divnu m) {
        >
        }
        >
        On the web page I have the following code:
        >
        <div id="div1" onclick="change div(1)">...</div>
        >
        This works OK.
        But if I try to use the more modern way of associating an event with
        an event handler, like,
        >
        function init() {
        document.getEle mentById('div1' ).onclick=chang ediv(1);
        }
        window.onload=i nit;
        >
        it doesn't work because of the parameter. How can I pass a parameter
        in this situation?
        >
        TIA
        >
        Paulo Almeida
        >
        >

        Comment

        Working...