views:

46

answers:

2

For some reason I only get to the first option when I alert out below and then it exists my entire function. It never iterates through each option:

    function SelectAlbumOption(iAlbumID, iAlbumDropdownID)
    {
        var dropdownID = '#' + iAlbumDropdownID;

        $(dropdownID).each(function(index, currentOption)
        {
            alert("(currentOption).attr('value'): " + $(currentOption).attr("value"));
            if($(currentOption).attr("value") == iAlbumID)
            {
                alert("matched option");
                $(currentOption).attr("selected", "yes");
                return false;
            }
        });
    }

right before this function above is called, I add options to that select, so they do exist before this function is called.

+6  A: 

To get the options you'll need to iterate trough them, not the <select> itself, like this:

var dropdownID = '#' + iAlbumDropdownID + ' option';

Currently it's iterating over the collection of <select> elements (only one of these) and giving you it's value. If you want the <option> your selector needs to go down to those.

Also you should set selected to true or "selected" instead of "yes" (though it gets converted to true behind the scenes anyway). One other note, this: $(currentOption).attr("value") can just be this.value, no need to wrap it in a jQuery object to get that property.


As an aside, it seems you've basically re-created the .val() function, is there any reason you're not just doing this?:

$('#' + iAlbumDropdownID).val(iAlbumID);

It should have the same effect as what you're currently doing, without the alerts (assuming those are for debugging).

Nick Craver
+1  A: 

Are you leaving critical pieces of the code out? Because you haven't told us whether you invoke SelectAlbumOption multiple times.

Your function takes an ID and does an .each on 1 element ( unique ID ).

Did you mean to do

    var dropdownID = '#' + iAlbumDropdownID;
    $('option', dropdownID).each();

Or

$(dropdownID + ' option').each()
meder
I only invoke it once, there's no need to invoke it multiple times as It's iterating through the collection once after the select's options are populated prior to calling this function.
CoffeeAddict
yea that's it, missing the 'option'. I wasn't familiar with adding 2 items to $() yet. thanks.
CoffeeAddict
For future purposes, you know you can use your browser's console to try stuff like this out? So you could see what elements your jQuery is selecting et al.
meder
you talking about such as the Firebug console?
CoffeeAddict
Yea I don't get how to use the Firebug console (what the hell would you even type for this console.log(what?) especially how it's going to help me in this case when I just was not aware of simple syntax.
CoffeeAddict
@CoffeeAddict - You should really look at the addition to my answer, there's a *much* easier way of doing this, taking your entire function down to one line.
Nick Craver
Not sure what you mean, I've got a function there and I need that logic within...so I don't see how it can possibly go down to one line with all the logic I have in the .each(). So you were talking about the Firebug console prior am I correct? I'm still trying to figure that thing out.
CoffeeAddict