user1447718
user1447718

Reputation: 681

selected to retrieve all dropdowns which has same selectedindex value

Each row in my table has a dropdown. the values in the dropdown are "Edit" and "Cancel". i want to write a jquery selector which will return me all dropdowns with has a selectedvalue as "Edit". how do i do it?

name of edit control is same for all rows. am forming edit control dynamically like this. below code is inside a loop.

    $tr.append(
                '<td>' +
                '<select id=\"editOption\">' +
                    '<option value=\"'+pkid+'_select\"></option>' +
                    '<option value=\"'+pkid+'\">Edit</option>' +
                    '<option value=\"'+pkid+'\">Cancel</option>' +
                '</select>' +
                '</td>'
        );

As per the suggestion i changed the code to

'<td>' + 
    '<select id=\"editOption_'+pkid+'\" class=\"editOption\">' + 
        '<option value=\"-1\"></option>' + 
        '<option value=\"Edit\">Edit</option>' + 
        '<option value=\"Cancel\">Cancel</option>' + 
        '</select>' + 
'</td>'

and when I use this:

var editedRows = $("table tr select").filter(function() { 
    return $(this).find("option").value == "Edit"; //This should be value, but for your code, its text() 
}); 
console.log(editedRows.length); 

length is coming as zero.

Upvotes: 1

Views: 955

Answers (2)

Ahmed Alaa El-Din
Ahmed Alaa El-Din

Reputation: 1833

This works fine it will alert if selected = Edit

var table = document.getElementById("prsnl");  
var rows = table.getElementsByTagName("tr"); 
for(j = 0 ; rows.length; j++){
var select = rows[j].getElementsByTagName("select");
    for(i = 0; i < select.length; i++){
        alert(select[i].value);
        if(select[i].value.indexOf("Edit") >= 0){
            alert('hi');
        }else{
            alert('fail');
        }
    }
}

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

You can use a filter function:

var editSelects = $("table tr select").filter(function() {
    return $(this).find("option").text() == "Edit"; //This should be value, but for your code, its text()
});

Also, per @JasonP comments, your ID's must be unique.

Upvotes: 2

Related Questions