views:

765

answers:

2

Hi. I just discovered something that I found odd. I have mys listbox with several store items [item ID, item name].

<select multiple="multiple" size="4" name="storeListBox" id="storeListBox">
<option value="11">item 1</option>
<option value="12">item 2</option>
<option value="13">item 3</option>
<option value="10">item 4</option>
</selec>

In my javascript I have the following code:

jQuery('#btnAddItem').click(function(){ addItemToStorageList(); });

function addItemToStorageList()
{
  var stores = jQuery('#storeListBox').val();
  alert(stores);
}

Upon selecting 3 items from the list and clicking the 'add' button, the alert displays '11,13,10'. But why is the alert triggered 3 times?

A: 

maybe try to put it in : $(document).ready(function(){

});
Haim Evgi
+1  A: 

Something else on your page is causing alert to appear three times. On mine the alert only appears once and shows 11,13,10 which is correct.

If you can find which other script is messing this one try this:

Just return false after calling the alrert

$(document).ready(function() {
        jQuery('#btnAddItem').click(function() { addItemToStorageList(); });

        function addItemToStorageList() {
            var stores = jQuery('#storeListBox').val();
            alert(stores);
            return false
        }
    });
Ali
Yeah, your right. I hadd added the .click function inside a $('#storeListBox').change (Add button should only be clickable after one or more items are selected). This caused this weird behaviour. I guess my brain gets kind of slow for coding 12 hours a day :) Thanks!
Steven