Problems calling a function

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

    Problems calling a function

    I have made a function createFirstMenu where I call "resetMenu" in a
    JavaScript. But nothing happens when I call resetMenu.

    function createFirstMenu (sel){
    sel = document.getEle mentById('sel1' );
    resetMenu(sel);
    }

    function resetMenu(sel){
    sel.length = 0;
    opt = document.create Element('OPTION ');
    sel.appendChild (opt);
    opt.value = "";
    opt.text = "-- Choose--";
    }



    This is the form where I call createFirstMenu when the page loads.

    <body onload="createF irstMenu('sel1' );">
    <form name="sels" method="post" action="">
    <select name="sel1" id="sel1" onchange="creat eSecondMenu('se l1','sel2');">
    </select>

    <select name="sel2" id="sel2" onchange="creat eThirdMenu('sel 1','sel2',
    'sel3');">
    <option value="">-- V&aelig;lg --</option>
    </select>

    <select name="sel3" id="sel3">
    <option value="">--V&aelig;lg--</option>
    </select>

    <input name="search" type="text" id="search">
    &nbsp;
    <input type="submit" name="Submit" value="Søg">
    </form>



    Hope someone can help!


  • JS

    #2
    Re: Problems calling a function

    Again it was because I overlooked an "{". I have wasted quite some hours on
    these syntax errors by now. I use Dreamweaver and for some reason this
    program don't have any syntax check (like eclipse) for javascript or JSP.

    Hopefully there are some better programs to write these things in, because
    syntax correction are one of the most basic features that a program should
    have. Any recommendations ??


    Comment

    • Evertjan.

      #3
      Re: Problems calling a function

      JS wrote on 30 mei 2005 in comp.lang.javas cript:
      [color=blue]
      > function createFirstMenu (sel){
      > sel = document.getEle mentById('sel1' );
      > resetMenu(sel);
      >}
      >[/color]

      Not that it matters,
      but why enter a parameter in the local variable

      sel

      --- function createFirstMenu (sel){

      and then immediately changing the content to

      document.getEle mentById('sel1' );

      --- sel = document.getEle mentById('sel1' );

      ?

      --
      Evertjan.
      The Netherlands.
      (Replace all crosses with dots in my emailaddress)

      Comment

      • Zif

        #4
        Re: Problems calling a function

        JS wrote:
        [...][color=blue]
        > Hopefully there are some better programs to write these things in, because
        > syntax correction are one of the most basic features that a program should
        > have. Any recommendations ??[/color]

        <URL:http://www.editplus.co m/>

        Gets my vote.

        --
        Zif

        Comment

        • RobG

          #5
          Re: Problems calling a function

          JS wrote:[color=blue]
          > Again it was because I overlooked an "{". I have wasted quite some hours on
          > these syntax errors by now. I use Dreamweaver and for some reason this
          > program don't have any syntax check (like eclipse) for javascript or JSP.
          >
          > Hopefully there are some better programs to write these things in, because
          > syntax correction are one of the most basic features that a program should
          > have. Any recommendations ??[/color]

          Firefox's JavaScript console is very helpful for syntax errors, usually
          providing a line number and giving a link to the source so you can
          easily fix them.

          For more serious stuff there's Mozilla's JavaScript debugger 'Venkman'

          <URL:http://www.mozilla.org/projects/venkman/>

          And Douglas Crockford's JavaScript verifier 'JSLint':

          <URL:http://www.crockford.c om/javascript/lint.html>


          Other Firefox developer tools (including extensions to the JavaScript
          console, HTML, CSS and RSS tools):


          <URL:https://addons.mozilla. org/extensions/showlist.php?ap plication=firef ox&category=Dev eloper%20Tools>

          Have fun.



          --
          Rob

          Comment

          • JS

            #6
            Re: Problems calling a function


            "Evertjan." <exjxw.hannivoo rt@interxnl.net > skrev i en meddelelse
            news:Xns9667ECE 6F160eejj99@194 .109.133.242...[color=blue]
            > JS wrote on 30 mei 2005 in comp.lang.javas cript:
            >[color=green]
            > > function createFirstMenu (sel){
            > > sel = document.getEle mentById('sel1' );
            > > resetMenu(sel);
            > >}
            > >[/color]
            >
            > Not that it matters,
            > but why enter a parameter in the local variable
            >
            > sel
            >
            > --- function createFirstMenu (sel){
            >
            > and then immediately changing the content to
            >
            > document.getEle mentById('sel1' );
            >
            > --- sel = document.getEle mentById('sel1' );
            >
            > ?[/color]

            I have this form:


            <body onload="createF irstMenu('sel1' );">
            <form name="sels" method="post" action="?page=j ubii">
            Kategori:&nbsp; &nbsp;&nbsp;
            <select name="sel1" id="sel1" onchange="creat eSecondMenu('se l1','sel2',
            'sel3');">
            </select>
            ....
            ..
            ..
            ..

            But for some reason I don't get access to sel1 in createFirstMenu if I don't
            explicitly grap it with:

            sel = document.getEle mentById('sel1' );

            Guess I am missing something?


            Comment

            • Zif

              #7
              Re: Problems calling a function

              JS wrote:[color=blue]
              > "Evertjan." <exjxw.hannivoo rt@interxnl.net > skrev i en meddelelse
              > news:Xns9667ECE 6F160eejj99@194 .109.133.242...
              >[color=green]
              >>JS wrote on 30 mei 2005 in comp.lang.javas cript:
              >>
              >>[color=darkred]
              >>>function createFirstMenu (sel){
              >>> sel = document.getEle mentById('sel1' );
              >>> resetMenu(sel);
              >>>}
              >>>[/color]
              >>
              >>Not that it matters,
              >>but why enter a parameter in the local variable
              >>
              >>sel
              >>
              >>--- function createFirstMenu (sel){
              >>
              >>and then immediately changing the content to
              >>
              >>document.getE lementById('sel 1');
              >>
              >>--- sel = document.getEle mentById('sel1' );
              >>
              >>?[/color]
              >
              >
              > I have this form:
              >
              >
              > <body onload="createF irstMenu('sel1' );">
              > <form name="sels" method="post" action="?page=j ubii">
              > Kategori:&nbsp; &nbsp;&nbsp;
              > <select name="sel1" id="sel1" onchange="creat eSecondMenu('se l1','sel2',
              > 'sel3');">
              > </select>
              > ...
              > .
              > .
              > .
              >
              > But for some reason I don't get access to sel1 in createFirstMenu if I don't
              > explicitly grap it with:
              >
              > sel = document.getEle mentById('sel1' );
              >
              > Guess I am missing something?[/color]

              The local variable 'sel' is created here:

              function createFirstMenu (sel){

              It is passed the string 'sel1' from the onload event:

              <body onload="createF irstMenu('sel1' );">

              The content of 'sel' is replaced with a reference to the element
              with id 'sel1' using a hard-coded parameter:

              sel = document.getEle mentById('sel1' );

              which seems to ignore the value passed to sel in the first place. To
              re-use the local variable 'sel', then:

              sel = document.getEle mentById(sel);

              would make more sense, but probably better would be to use the forms
              collection and reduce reliance on getElementById (although it is
              supported by perhaps 95% of browsers in use):

              sel = document.forms['sels'].elements[sel];

              or a reference to the required element could be passed directly from
              the onload event:

              <body onload="
              createFirstMenu (document.forms['sels'].elements['sel1']);
              ">

              Thereby removing the hard-coded values from the script. Having gone
              that far, the usefulness of createFirstMenu () becomes moot, since all
              it does is call resetMenu() - the onload is effectively:

              <body onload="
              resetMenu(docum ent.forms['sels'].elements['sel1']);
              ">

              The resetMenu() function is:

              function resetMenu(sel){
              sel.length = 0;
              opt = document.create Element('OPTION ');
              sel.appendChild (opt);
              opt.value = "";
              opt.text = "-- Choose--";
              }

              When modifying the length of the select to change the number of
              options, it seems appropriate to set it to the new length directly:

              sel.length = 1;

              and then modify the remaining option(s):

              sel[0].value = "";
              sel[0].text = "-- Choose--";

              so now you have:

              function resetMenu(sel){
              sel.length = 1;
              sel[0].value = "";
              sel[0].text = "-- Choose--";
              }

              If there are many option attributes to modify, it may be better
              (faster) to create a local variable with a reference to the option:

              function resetMenu(sel){
              sel.length = 1;
              var s = sel[0];
              s.value = "";
              s.text = "-- Choose--";
              //...
              }




              --
              Zif

              Comment

              • Random

                #8
                Re: Problems calling a function

                Zif wrote:[color=blue]
                > JS wrote:
                > [...][color=green]
                > > Hopefully there are some better programs to write these things in, because
                > > syntax correction are one of the most basic features that a program should
                > > have. Any recommendations ??[/color]
                >
                > <URL:http://www.editplus.co m/>
                >
                > Gets my vote.
                >
                > --
                > Zif[/color]

                Aside from vi/vim, EditPlus is my only text editor.
                Never heard anyone else mention it but it's the best I've seen.

                Comment

                • JS

                  #9
                  Re: Problems calling a function

                  I am still confused about the way arguments are passed. In my test.jsp page
                  I have:


                  <body onload="createF irstMenu('sel1' );">
                  <form name="sels" method="post" action="?page=j ubii">
                  Kategori:&nbsp; &nbsp;&nbsp;
                  <select name="sel1" id="sel1" onchange="creat eSecondMenu('se l1','sel2',
                  'sel3');">
                  </select>
                  &nbsp;&nbsp;&nb sp;&nbsp;Kriter ium:&nbsp;&nbsp ;
                  <select name="sel2" id="sel2" onchange="creat eThirdMenu('sel 1','sel2',
                  'sel3');">
                  <option value="0">-- V&aelig;lg --</option>
                  </select>
                  </form>



                  And in my JavaScript I have:

                  function createFirstMenu (sel){
                  sel = document.getEle mentById(argume nts[0]);

                  for(i=0;entry.l ength>i;i++){
                  opt = document.create Element('OPTION ');
                  sel.appendChild (opt);
                  opt.value = i+1;
                  opt.text = entry[i][0];
                  }
                  }


                  If I call createFirstmenu with "sel2" nothing changes:

                  <body onload="createF irstMenu('sel2' );">
                  ....
                  ....
                  ....

                  For some reason it does not matter which argument I give createFirstMenu in
                  test.jsp. I have even tried:

                  <body onload="createF irstMenu('bla') ;">

                  and the first menu still gets vreated as it should. What am I missing?


                  Comment

                  • Lee

                    #10
                    Re: Problems calling a function

                    JS said:[color=blue]
                    >
                    >I am still confused about the way arguments are passed. In my test.jsp page
                    >I have:
                    >
                    >
                    ><body onload="createF irstMenu('sel1' );">
                    ><form name="sels" method="post" action="?page=j ubii">
                    >Kategori:&nbsp ;&nbsp;&nbsp;
                    ><select name="sel1" id="sel1" onchange="creat eSecondMenu('se l1','sel2',
                    >'sel3');">
                    ></select>
                    >&nbsp;&nbsp;&n bsp;&nbsp;Krite rium:&nbsp;&nbs p;
                    ><select name="sel2" id="sel2" onchange="creat eThirdMenu('sel 1','sel2',
                    >'sel3');">
                    > <option value="0">-- V&aelig;lg --</option>
                    ></select>
                    ></form>
                    >
                    >
                    >
                    >And in my JavaScript I have:
                    >
                    >function createFirstMenu (sel){
                    > sel = document.getEle mentById(argume nts[0]);[/color]

                    Why do you do that? There's no need to use the arguments array
                    unless you don't know how many arguments you need to handle.

                    function createFirstMenu (selID) {
                    var sel = document.getEle mentById(selID) ;

                    should be easier to understand.

                    However, you could simplify it even further by passing a
                    reference to the Select object in the first place, so you
                    don't need to look it up by ID value:

                    onload="createF irstMenu(docume nt.sels.sel1)"

                    Comment

                    • JS

                      #11
                      Re: Problems calling a function

                      > <body onload="[color=blue]
                      > createFirstMenu (document.forms['sels'].elements['sel1']);
                      > ">
                      >
                      > Thereby removing the hard-coded values from the script. Having gone
                      > that far, the usefulness of createFirstMenu () becomes moot, since all
                      > it does is call resetMenu() - the onload is effectively:
                      >
                      > <body onload="
                      > resetMenu(docum ent.forms['sels'].elements['sel1']);
                      > ">
                      >
                      > The resetMenu() function is:
                      >
                      > function resetMenu(sel){
                      > sel.length = 0;
                      > opt = document.create Element('OPTION ');
                      > sel.appendChild (opt);
                      > opt.value = "";
                      > opt.text = "-- Choose--";
                      > }[/color]


                      Now I have:

                      <body onload="createF irstMenu('sel1' );">

                      and in my script I just have:

                      function createFirstMenu (sel){
                      resetMenu(sel);
                      ....
                      .....
                      ....
                      ....
                      }

                      And there is no need to use document.getEle mentById(argume nts[0]);

                      And it seems to work fine. I don't see any reason to make the call:


                      <body onload="
                      createFirstMenu (document.forms['sels'].elements['sel1']);">


                      it works fine when I just type:

                      <body onload="createF irstMenu('sel1' );">





                      Comment

                      • RobG

                        #12
                        Re: Problems calling a function

                        JS wrote:
                        [...][color=blue]
                        >
                        > Now I have:
                        >
                        > <body onload="createF irstMenu('sel1' );">
                        >
                        > and in my script I just have:
                        >
                        > function createFirstMenu (sel){
                        > resetMenu(sel);[/color]
                        [...][color=blue]
                        >
                        > And there is no need to use document.getEle mentById(argume nts[0]);[/color]

                        That was never suggested, it was your invention and it's a bad idea.
                        [color=blue]
                        >
                        > And it seems to work fine. I don't see any reason to make the call:[/color]

                        Looks can be deceiving, see below.
                        [color=blue]
                        > <body onload="
                        > createFirstMenu (document.forms['sels'].elements['sel1']);">[/color]

                        Because that is standards-compliant and will work reliably in a greater
                        range of browsers than your IE-centric alternative.
                        [color=blue]
                        >
                        > it works fine when I just type:
                        >
                        > <body onload="createF irstMenu('sel1' );">
                        >[/color]

                        Using element names & ID's as global variables has very limited support
                        outside Internet Explorer [1].

                        Please read the above posts thoroughly, you have been provided with a
                        detailed explanation of what you should be doing. Continually posting
                        garbage that ignores advice provided will cause most here to ignore
                        you.

                        [1] Noted that the Mozilla project has bowed under the weight of
                        non-W3C compliant sites to some extent and allows names and IDs to
                        be accessed as global variables in quirksmode.

                        <URL:https://bugzilla.mozill a.org/show_bug.cgi?id =256932>


                        --
                        Rob

                        Comment

                        Working...