tags:

views:

16

answers:

1

Hi, I have the following code:

$("input[id$=UserField_hiddenSpanData],input[title=Title]").each(function(){

    var rb = $('#ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_SelectResult option').length;
    var val = $(this).val();
    if(val != 0 && val.length != 0 && rb != 0) { 

        //add one to the counter
        controlsPassed += 1;
    }

    });

It works fine but I don't want to have the ID hardcoded so I thought I could use

var rb = $('id$=SelectResult option').length;

but it doesn't work, what's wrong with my syntax?

Thanks in advance.

+1  A: 

You need brackets on your ends-with selector, like this:

var rb = $('[id$=SelectResult] option').length;

It'll be faster with the element selector on there though:

var rb = $('select[id$=SelectResult] option').length;

Another thought as stated in comments on a prior question, there's no need to repeat this check every time in the loop, you can do it outside once :)

Nick Craver
thanks for that nick
Peter