0

i have made a little javascript script that sets a value (named here stock), which i want to make maximum in a drop down list. it will be the maximum value if it is greater that 6.

Here is the code:

<script type="text/javascript"> 
function selecteazaElement(id,stock)
{
document.addtobasket.idOfSelectedItem.value=id;
var number23=document.addtobasket.number;
number23.options.length=0;
if (stock>=6)
    stock=6;
for (i=1;i<=stock;i++)
{
    //alert ('id: '+id+'; stock: '+stock);
    number23.options[number.options.length]=new Option(i, i);
}
    //window.status="my status";
    } 
  </script>

i use this code in this form:

     <form id="addtobasket" name="addtobasket" method="POST" action="<?= Route::url('Add         to Basket', array('sale_id' => $sale->id)); ?>">
     <input type="hidden" name="idOfSelectedItem" id="idOfSelectedItem" value="-1">     
    <select name="number" id="number">                 
<option value=0>Choose the number of products</option>                                                         </select>
    <button type="submit" name = "submit" onclick="valbutton(addtobasket");>Adauga in cos</button><br />    
   </fieldset>
   </form>

So after the user chooses a product,form a list he can also choose a number(of products) my problem is:

It works perfectly in chrome and opera, but doesn't display the numbers in the drop down list in Firefox. i wonder why is that?

Thank you!

6
  • 2
    @dana Your HTML code is invalid. You're missing the <fieldset> tag. Apr 4, 2011 at 15:25
  • 1
    @dana Also, this is invalid: onclick="valbutton(addtobasket");. It should be like so: onclick="valbutton(addtobasket);". Apr 4, 2011 at 15:26
  • Please show how and where you are calling the script Also why <button type="submit" that does not look right and for sure do not have an onclick on a submit button. Instead put the call in the onsubmit of the form
    – mplungjan
    Apr 4, 2011 at 15:28
  • @dana When and where is selecteazaElement called? Apr 4, 2011 at 15:29
  • 2
    @Pointy I've figured it out. In Chrome and Opera, named elements (elements with a name attribute) become global properties, so number.options.length from OP's code works. In Firefox, that doesn't work - document.number does point to the SELECT element, but just number doesn't (unlike Chrome, Opera, ...). Apr 4, 2011 at 15:53

1 Answer 1

3

This line is causing the problem in Firefox:

number23.options[number.options.length]=new Option(i, i);

Change it to:

number23.options[number23.options.length]=new Option(i, i);

and it will work.

Your original code: http://jsfiddle.net/CS6uv/1/ (doesn't work in Firefox)

Corrected code: http://jsfiddle.net/CS6uv/2/ (works in Firefox)

1
  • hello, now i have a problem with this code in Safari, it doesn't work properly. any suggestions? thank you!
    – dana
    Apr 26, 2011 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.