1

I have a table and in every row I have a cell (Example, for first row: (id: ctl00_MainContent_grdSearch_i0_c5). I need to check if this cell has value 1 then disable its row (i.e. ctl00_MainContent_grdSearch_i0 in above case).

(id: ctl00_MainContent_grdSearch_itemsHolder)

row (id: ctl00_MainContent_grdSearch_i0)
row (id: ctl00_MainContent_grdSearch_i1)
row (id: ctl00_MainContent_grdSearch_i2)
9
  • 1
    What have you tried so far? Please post some actual code and any relevant HTML. Commented Dec 19, 2011 at 17:03
  • what is the "table" ? HTML ? and what are you trying to disable ? an input / anchor ? Commented Dec 19, 2011 at 17:03
  • What do you mean by disable the row? Do you want to disable all controls in a particular row if a particular cell has a value of 1? Commented Dec 19, 2011 at 17:04
  • @ManseUK It's an asp.net gridview but as it is rendered it appears as a html <div> table in browser. Commented Dec 19, 2011 at 17:05
  • 1
    Well if it was working, you wouldn't be asking a question about it. Please add more detail to your question instead of repeating yourself in comments. Commented Dec 19, 2011 at 17:08

3 Answers 3

3

You can use a single line to do this with the :contains() selector. This example is assuming you're using div elements the way you described in comments.

$( "#ctl00_MainContent_grdSearch_itemsHolder > div:contains(1)" )
    .attr( "disabled", "disabled");

If your HTML is a more complicated, you might want to be more specific and use the this line instead:

$( "#ctl00_MainContent_grdSearch_itemsHolder > div[id^=ctl00_MainContent_grdSearch_i]:contains(1)" )
    .attr( "disabled", "disabled");

The first line finds any div under your itemsHolder div with the value of 1. The second line only finds div elements with the id starting with ctl00_MainContent_grdSearch_i that have the value of 1.

In this demo I set background-color: gray; for the disabled rows so you could see them.

Demo: http://jsfiddle.net/ThinkingStiff/MULSv/

HTML:

<div id="ctl00_MainContent_grdSearch_itemsHolder">
    <div id="ctl00_MainContent_grdSearch_i0">
        <div id="ctl00_MainContent_grdSearch_i0_c0" class="item">0</div>
        <div id="ctl00_MainContent_grdSearch_i0_c1" class="item">0</div>
        <div id="ctl00_MainContent_grdSearch_i0_c2" class="item">0</div>
    </div>
    <div id="ctl00_MainContent_grdSearch_i1">
        <div id="ctl00_MainContent_grdSearch_i1_c0" class="item">0</div>
        <div id="ctl00_MainContent_grdSearch_i1_c1" class="item">1</div>
        <div id="ctl00_MainContent_grdSearch_i1_c2" class="item">0</div>
    </div>
    <div id="ctl00_MainContent_grdSearch_i2">
        <div id="ctl00_MainContent_grdSearch_i2_c0" class="item">1</div>
        <div id="ctl00_MainContent_grdSearch_i2_c1" class="item">0</div>
        <div id="ctl00_MainContent_grdSearch_i2_c2" class="item">0</div>
    </div>
    <div id="ctl00_MainContent_grdSearch_i3">
        <div id="ctl00_MainContent_grdSearch_i3_c0" class="item">0</div>
        <div id="ctl00_MainContent_grdSearch_i3_c1" class="item">0</div>
        <div id="ctl00_MainContent_grdSearch_i3_c2" class="item">0</div>
    </div>
</div>

CSS:

.item {
    display: inline-block; 
}

Script:

$( "#ctl00_MainContent_grdSearch_itemsHolder > div:contains(1)" )
    .attr( "disabled", "disabled").css( "background-color", "gray" );

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

2

Try the following:

$('tr').find('td[id^="ctl00_MainContent_grdSearch"]').each(function() {
  var self = $(this);
  if (self.text() === '1') {
    self.closest('tr').attr('disabled', 'disabled');
  }
});

6 Comments

Thanks a lot for your guidance but I noticed that it is comming in <div> instead of table :-( is this going to effect ?
@Zerotoinfinite Yes, it will have an effect. Please post the HTML that you wish to manipulate and I'll update accordingly.
it is like this <div id="ctl00_MainContent_grdSearch_i0" <div id=" ctl00_MainContent_grdSearch_itemsHolder" and <div id=" ctl00_MainContent_grdSearch_i0_c6"
I think I have confused you.. it is like foreach div in ctl00_MainContent_grdSearch_itemsHolder (this will get me the row inside the ctl00_MainContent_grdSearch_itemsHolder)
and foreach inside the above foreach, i.e. for each row ctl00_MainContent_grdSearch_i0 we need to find items, and based on the control text tl00_MainContent_grdSearch_i0_c6 we need to make complete row [i.e. ctl00_MainContent_grdSearch_i0] disabled
|
1

You said the table was really divs, so this works. See the fiddle here, http://jsfiddle.net/nickyt/XVttD

var rows = $("div[id*='MainContent_grdSearch_i0_c']");

rows.each(function() {
    var row = $(this);

    if (row.text() === "1") {
        row.attr('disabled', 'disabled');
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.