code for select box is not working in mozilla firefox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mukeshrasm
    Contributor
    • Nov 2007
    • 254

    code for select box is not working in mozilla firefox

    hi

    I am passing the value of select to a function which is called on click event of button. here is the code

    [code=html]
    <select name="ctName" style="width:30 0px;" id="clientname " >
    <option selected="selec ted" value="">----Select Client Name----</option>
    <option value="mukesh"> mukesh</option>
    <option value="kumar">k umar</option>
    <option value="mishra"> mishra</option>

    <input type="button" name="Submit2" value="GO" id="bgo" onclick="advanc eResults(ctName .options[ctName.selected Index].value" />
    [/code]

    Here I am using ajax and function advanceResults( ) is called in .js page. though this code works fine in IE but it is not working in Mozilla Firefox.

    and same is happening when I am passing value of Text box to other button
    Last edited by Dormilich; Jun 30 '09, 06:06 AM. Reason: fixed code tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    out of the blue I'd say your JS function is too IE specific, but to be sure I need to see the source code.

    Comment

    • mukeshrasm
      Contributor
      • Nov 2007
      • 254

      #3
      Originally posted by Dormilich
      out of the blue I'd say your JS function is too IE specific, but to be sure I need to see the source code.
      here is the source code

      [code=html]
      <head>
      <script src="comsel.js" type="text/javascript"></script>
      <meta name="Content-Script-Type" content="text/javascript" />
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>demo</title>
      </head>
      <select name="sel" >
      <option selected="selec ted">select</option>
      <option value="mukesh"> mukesh</option>
      <option value="kumar">k umar</option>
      <option value="mishra"> mishra</option>
      </select>
      <input type="button" onclick="change (sel.options[sel.selectedInd ex].value)" value="submit" />
      <span id="searchResul t"></span>
      [/code]

      [code=JavaScript]
      var xmlHttp
      function change(str)
      {
      xmlHttp=GetXmlH ttpObject()
      if (xmlHttp==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
      var url="comsell.ph p";
      url=url+"?q="+s tr;
      url=url+"&sid=" +Math.random();
      xmlHttp.onready statechange=sta teChanged ;
      xmlHttp.open("G ET",url,true) ;
      xmlHttp.send(nu ll);
      }
      function stateChanged()
      {
      if (xmlHttp.readyS tate==4 || xmlHttp.readySt ate=="complete" )
      {
      document.getEle mentById("searc hResult").inner HTML=xmlHttp.re sponseText;
      }
      }
      function GetXmlHttpObjec t()
      {
      var xmlHttp=null;
      try
      {
      xmlHttp=new XMLHttpRequest( );
      }
      catch (e)
      {
      try
      {
      xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" );
      }
      catch (e)
      {
      xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP");
      }
      }
      return xmlHttp;
      }
      [/code]
      Last edited by Dormilich; Jun 30 '09, 07:01 AM. Reason: fixed [code] tags

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Code:
        sel.options[sel.selectedIndex].value
        what does that return in FF?

        PS: the correct use of code tags is decribed here

        Comment

        • mukeshrasm
          Contributor
          • Nov 2007
          • 254

          #5
          Originally posted by Dormilich
          Code:
          sel.options[sel.selectedIndex].value
          what does that return in FF?

          PS: the correct use of code tags is decribed here
          it returns nothing in Firefox but gives
          Error: sel is not defined
          Source File: http://localhost/comsel.html
          Line: 1

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by mukeshrasm
            it returns nothing in Firefox but gives
            Error: sel is not defined
            kind of expected that. if you don't have a form element try:
            Code:
            var sel = document.getElementsByName("sel")[0];
            sel.options[sel.selectedIndex].value

            Comment

            • mukeshrasm
              Contributor
              • Nov 2007
              • 254

              #7
              Originally posted by Dormilich
              kind of expected that. if you don't have a form element try:
              Code:
              var sel = document.getElementsByName("sel")[0];
              sel.options[sel.selectedIndex].value
              I changed the code like

              Code:
              document.getElementsByName('sel')[0].options[sel.selectedIndex].value
              but still it is not working since this is inside the function so I have to change the double to single quote

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by mukeshrasm
                I changed the code like
                Code:
                document.getElementsByName('sel')[0].options[[B]sel[/B].selectedIndex].value
                but still it is not working
                same reason as above. you have sel not defined (you use this var twice!) that's why I used to define sel beforehand.

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  you could avoid this problem by using an external script file (though that requires the use of event listeners)

                  Code:
                  function change()
                  {
                      var sel = document.getElementsByName("sel")[0];
                      var str = sel.options[sel.selectedIndex].value;
                      // …
                  }
                  
                  // replace onclick="…" by id="submit" in the submit button
                  document.getElementById("submit").addEventListener("click", change, false);
                  // disclaimer: this won't work in IE, because they don't conform to the standards, google for the addEvent() cross-browser event listeners

                  Comment

                  • mukeshrasm
                    Contributor
                    • Nov 2007
                    • 254

                    #10
                    Originally posted by Dormilich
                    you could avoid this problem by using an external script file (though that requires the use of event listeners)

                    Code:
                    function change()
                    {
                        var sel = document.getElementsByName("sel")[0];
                        var str = sel.options[sel.selectedIndex].value;
                        // …
                    }
                    
                    // replace onclick="…" by id="submit" in the submit button
                    document.getElementById("submit").addEventListener("click", change, false);
                    // disclaimer: this won't work in IE, because they don't conform to the standards, google for the addEvent() cross-browser event listeners
                    thanks for kind effort to resolve the problem and for suggestion.

                    Comment

                    Working...