views:

79

answers:

3

I've kind of written myself into a corner, and was hoping there was an "easy" way out. I'm trying to loop through a series of things on my page, and build a key:value pair. Here is my structure:

<div class="divMapTab" id="divMapTab34">
    <div class="divFieldMap">
        <select class="selSrc" id="selTargetnamex"><options....></select>
    </div>
</div>
<div class="divMapTab" id="divMapTab87">
    <div class="divFieldMap">
        <select class="selSrc" id="selTargetnamex"><options....></select>
    </div>
</div>

It's way more complicated than that, and there are many select elements inside of each divFieldMap div.

Here is my JS function that is building my string:

    function Save() {
        var sSaveString = '';

        $('.divMapTab').each(function() {
            var thisId = this.id;
            $('.selSrc', "#" + thisId).each(function() {
                var thisSubId = this.id;
                //alert(thisSubId);   <-- HERE IS THE PROBLEM
                var sTargetCol = thisSubId.replace('selTarget', '');
                var sValue = this.val();
                sSaveString += sTargetCol + '¸' + sValue + '·';
            });
        });
    }

On the line that has the alert box and the text "HERE IS THE PROBLEM" is that I'm trying to get the selected value of the "current" select input element, but the id of that element isn't unique (I thought it would be, but I screwed up). Is there a good way, inside of an "each" type of jQuery statement, to use "this" to get the exact select element that I really am looking for, even if it doesn't have a unique id?

+1  A: 

I believe you can create a JQuery reference to the object using $(this). Is that what you are after?

RobM
I didn't realize that $(this) was different than $('#' + this.id). However, they are VERY different if you an idiot like me and accidentally get duplicate ids.
Matt Dawdy
`$(this)` is *completely* different. The `this` operator is a read-only reference to the execution context of a function, and *is not fixed for a function*. https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/this_Operator
Matt Ball
Beas will eat you -- I'm not sure I completely follow. I know that "this" changes depending on what called what, etc, but in my code above, I can count on "this" referring to current object in an iteration of the matched elements from this selector: $('.selSrc', "#" + thisId). Right?
Matt Dawdy
+3  A: 

Valid HTML element IDs must be unique. If your document contains duplicate IDs, all bets are off.

If you really can't correctly generate unique IDs, start using classes instead.

Matt Ball
Actually, they do not _have_ to be unique. Of course it was making my life !hell! because they weren't (I thought they would be but I didn't realize that my items could be coming from different objects in my data structure, so I assumed that there wouldn't be repeats.) Browsers happily let you reuse ids, and will return the first element found with that id via the getElementById function. jQuery happily returns an array of all that match.
Matt Dawdy
@Matt: what version of jQuery are you using? My jQuery sure as hell doesn't happily give me an array of all that match.
Matt Ball
I just tried this: alert($('#' + this.id).length); and got back 1 -- you are right, it isn't returning all that match.I've gotten myself so turned around messing with this page. It doesn't blow up, but it sure as hell isn't getting the value that I wanted. I for some reason thought that it was returning me 2 items, but getting the value out of the first, which happened to be the one that I didn't want. My head is spinning, I need a drink. Thanks for helping me.
Matt Dawdy
+1  A: 

So, you just want to find the value of the selects inside each div?

var myOptions = [];

$('.divMapTab').each(function()
{
    $(this).children('select').each(function()
    {
        myOptions.push(this.id, $(this).val());
    });
});

myOptions would then contain a key value pair from each select Id to it's value.

Tejs