Samantha J T Star
Samantha J T Star

Reputation: 32808

How can I set specific elements to be disabled?

I have:

var row = 22;

How can I set it so that all my input and select elements that have IDs where XXX can be any combination of characters are disabled:

modal_XXX_22 

Upvotes: 1

Views: 67

Answers (5)

Ram
Ram

Reputation: 144689

You can try Multiple Attribute selector:

$('*[id^="modal_"][id$="_'+row+'"]').prop('disabled', true)
// ^-  ^--           ^--- attribute ends with selector
//  |    |     
//  |    --- attribute starts with selector 
//  ---- all selector     
  1. All Selector
  2. Attribute Starts With Selector
  3. Attribute Ends With Selector

Upvotes: 1

inhan
inhan

Reputation: 7470

$('input[id^="modal_"][id$="_'+N+'"],select[id^="modal_"][id$="_'+N+'"]')
.attr('disabled','disabled');

Upvotes: 0

Ayush
Ayush

Reputation: 42450

You can iterate over all children and check if the id contains the string you want. Like this:

$(document).ready(function(){
    $('ul').children().each(function(){
        var id = $(this).attr('id');
        if (typeof(id) !== 'undefined') {
            if (id.indexOf('22') != -1) {
                $(this).css('color','red');
            }
        }
    });

});​

If you want it to run on your entire file, you can change it to

$('body').children()...

Live demo

Upvotes: 0

zerkms
zerkms

Reputation: 254926

That is how you would select them:

$('[id^="modal_"][id$="_' + row + '"]').

Upvotes: 2

iambriansreed
iambriansreed

Reputation: 22241

Try:

$(':input[id^="modal_"][id*="_'+ row +'_"]')

Though a better HTML setup would be single element wrapping inputs.

Upvotes: 0

Related Questions