Paul On Technology

Exploring technology

JQuery selectors – selecting elements by a partial id

Recently a coworker had a grid like the one below in an ASP.Net page. He wanted me too see if there was any way to make the javascript the manages the checkboxes easier to maintain.

CheckboxGrid +

The checkboxes on the left check or uncheck the day check boxes for the corresponding row. When the checkbox next to Barney Rubble is checked the grid looks like this:

CheckboxGridDays

The SO button checks all signoff checkboxes in the grid and when it is clicked the grid looks like this:

CheckboxGridDaysSO

The original programmer had a load of javascript to select the day checkboxes for the row, and to select all of the signoff checkboxes he did a post back to do the work on the server.

Right away I knew this would be easy to do with JQuery. The only problem that I had was how to get just the day checkboxes or just the signoff checkboxes. Luckily, all of the day checkbox Ids had the word “Day” in them and the signoff checkboxes had the words “IsSignedOff” in their Ids.

Here is how I check all of the day checkboxes in the row that the select all checkbox resides in:

The function call from the select all checkbox’s onclick event:

 

<input id="ctl00_ContentPlaceHolder1_grdTest_ctl03_SelectAll" type="checkbox" onclick="javascript:onSelectAll(this);" name="ctl00$ContentPlaceHolder1$grdTest$ctl03$SelectAll" />

I pass “this”, which is the checkbox, to the function. Now for the function that does the work:

function

onSelectAll(checkbox) {

 

var cb = $("#" + checkbox.id);

 

var td = cb.parent("td");

 

var tr = td.parent("tr");

tr.children().children(

"input:checkbox[id*=Day]").attr('checked', cb.attr('checked'));

}

Let’s break the function down. It has to get checkboxes with html like this:

<td style="border-color: Black; border-style: Solid;">

<input id="ctl00_ContentPlaceHolder1_grdTest_ctl03_Day1" type="checkbox" name="ctl00$ContentPlaceHolder1$grdTest$ctl03$Day1" />

</td>

The actual select all checkbox is passed as an argument. I then get the JQuery handle to that checkbox by using a JQuery id selector built using the actual id of the checkbox passed in.

Next I get the TD that contains the select all checkbox using the JQuery parent function on the checkbox. Then I get the TD’s parent row using the JQuery parent function on the TD.

Now that I have the rows I can get all of the checkboxes that are descendent children of that row, but I only want the checkboxes that contain the string “Day” in their ids. Remember, that last checkbox in the row has “IsSignedOff” in it’s id and the first one in the row is the select all checkbox.

Using a css selector of input:checkbox[id*=Day] says give me the input controls that are of type checkbox that have an id that contains the string “Day”. The *= is the JQuery operator for contains.

So this function gets me all of the day checkboxes and sets their checked attribute to the checked attribute of the select all checkbox that initiates the function call.

The last piece is to get the is signed off checkboxes in all rows, one per row. This is really easy. The button html looks like this:

<input type=”button” onclick="javascript:checkSignOffs();" value=”Sign Off” />

The function is this:

function

checkSignOffs() {

$("#grdTest input:checkbox[id*=IsSignedOff]").attr('checked', true);

}

It has to get checkboxes with html like this:

 

<input id="ctl00_ContentPlaceHolder1_grdTest_ctl03_IsSignedOff" type="checkbox" name="ctl00$ContentPlaceHolder1$grdTest$ctl03$IsSignedOff" />

The selector says for a table with an id of “grdTest”, get me the input controls of type checkbox that have an id that contains the string “IsSignedOff”. When JQuery gets the set of checkboxes, set the checked attribute of each one to checked.

Not  bad for a few lines of code. Now maintenance will be much easy. We ripped out alot of javascript, removed a post back and best of all, I now have JQuery in another application.

If anyone has a cleaner, more concise way of doing this please leave a comment. I know I can get the table row in the first set of code easier, I just need to figure it out. I will post an update when I do. I was in a hurry to share this :0)

Comments

Maryam said:

This post is really an angel for me.. It solved my great problem

# August 31, 2009 1:51 AM

Rick O'Shay said:

This reads like a really long-winded description of the "contains" operator. Regarding the onclick attributes, convention dictates those should never be mixed in with markup when using jQuery;

# September 18, 2009 11:22 AM

Paul Speranza said:

@Rick - I solved a problem that a coworker had without making him redo his page. I don't see how this is long winded, I gave a good example of the problem and the solution.

Please share with the community how you would have done it.

Thanks for commenting.

# September 21, 2009 1:16 PM

Ayah said:

You really ROCK!!!... saved my day ;)

# March 30, 2010 5:49 AM

TK said:

// no change here except for avoiding the onclick attribute

$('input[value=SO]').bind('onclick', function() {

$("#grdTest input:checkbox[id*=IsSignedOff]").attr('checked', true);

});

// this one is a little different * completely untested but you get the idea

$('input[value*=SelectAll]').bind('onclick', function() {

var $this = $(this);

var checked = $this.attr(checked);

$this.parents('tr:first').find(input[id*=Day]).attr('checked', checked );

});

# September 12, 2010 9:39 PM

Waqar Ahmed said:

Very useful JQuery tip for manipulating .net generated element ID's !

# February 17, 2011 7:13 AM

generic viag  said:

excellent work!!!, thanks

# March 24, 2011 5:42 AM

baseball caps said:

the style we mainly found was Stencil Graffiti Art as well plenty of Murals and other similar intensive artworks. Not just text and type. <a href="www.baseballcaps-forsale.com/" title="baseball caps">baseball caps</a>

# July 25, 2011 5:22 AM

Cheap Supra Footwear said:

I agree with your Blog and I will be back to check it more in the future so please keep up your work. I love your content & the way that you write. It looks like you have been doing this for a while now, how long have you been blogging for?<a href="www.supratksocietyblue.com/" title="Cheap Supra Footwear">Cheap Supra Footwear</a>

# July 26, 2011 8:45 PM

cheap Timberland boots said:

Its really good to be part of a community that you know you can contribute something worthwhile. thanks for the share.<a href="www.cheaptimberlandboots-outlet.com/" title="cheap Timberland boots">cheap Timberland boots</a>

# July 27, 2011 3:35 AM

Cheap Supra Footwear said:

I agree with your Blog and I will be back to check it more in the future so please keep up your work. I love your content & the way that you write. It looks like you have been doing this for a while now, how long have you been blogging for?<a href="www.supratksocietyblue.com/" title="Cheap Supra Footwear">Cheap Supra Footwear</a>

# July 27, 2011 9:45 PM

Birkenstock sandals said:

Very nice and helpful information has been given in this article. I must say that this is a very good post.<a href="www.birkenstocksandalscheapsale.com/" title="Birkenstock sandals">Birkenstock sandals</a>

# July 29, 2011 5:06 AM

burberry bags outlet online said:

you mean you dont need a special tool? Thats refreshing! Thanks!<a href="www.burberrybagsoutletonline.com/" title="burberry bags outlet online">burberry bags outlet online</a>

# July 29, 2011 5:19 AM

belstaff leather jackets said:

Thanks for sharing. i really appreciate it that you shared with us such a informative post.<a href="www.belstaffleatherjacketsuk.com/" title="belstaff leather jackets">belstaff leather jackets</a>

# August 1, 2011 10:36 PM

discount gucci bags said:

Thanks a lot for enjoying this beauty article with me. I am enjoy it very much! Looking forward to another great article. Good luck to the author! all the best!<a href="www.discountguccibagssales.com/" title="discount gucci bags">discount gucci bags</a>

# August 2, 2011 2:58 AM

designer bags said:

I've always appreciated internet as it is such a great method of obtaining information and this very article simply proove me right. Cheers as well as hip hip to net concept.<a href="www.buy-designerbags.com/" title="designer bags">designer bags</a>

# August 2, 2011 8:18 PM

cheap Chanel bags said:

Thanks for the Blog, thanks for helping me with your useful Article. I think it is really a great topic to write about on my blog.<a href="www.cheapchanelbags-outlet.com/" title="cheap Chanel bags">cheap Chanel bags</a>

# August 2, 2011 11:24 PM

buy nike air max said:

Today well lived makes every yesterday a dream of happiness and every tomorrow a vision of hope. Look well therefore to this day.<a href="www.buynike-airmax.com/" title="buy nike air max">buy nike air max</a>

# August 3, 2011 4:59 AM

chanel outlet said:

You managed to hit the nail upon the top as well asand alsoand defined out the whole thing without having side effectside-effects , people cancould take a signal. Will likelyprobably be back to get more. Thanks<a href="www.authentic-chanel-outlet.com/" title="Chanel Outlet ">Chanel Outlet</a>

# August 3, 2011 6:40 AM

chanel outlet said:

You managed to hit the nail upon the top as well asand alsoand defined out the whole thing without having side effectside-effects , people cancould take a signal. Will likelyprobably be back to get more. Thanks<a href="www.authentic-chanel-outlet.com/" title="Chanel Outlet ">Chanel Outlet</a>

# August 3, 2011 10:42 AM

Timberland boots outlet said:

Very nice post. I really enjoy the reading. I  come here from the google while searching for some good article.Thanks <a href="www.cheap-timberlandbootsoutlet.com/" title="Timberland boots outlet">Timberland boots outlet</a>

# August 4, 2011 5:53 AM

fashion bags said:

I am happy to have found this blog, and more happy to have long tail explained so well. Thanks.<a href="www.fashion-bags-store.com/" title="fashion bags">fashion bags</a>

# August 5, 2011 2:11 AM

burberry bags outlet said:

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.<a href="www.burberrybagsoutletsale.com/" title="burberry bags outlet">burberry bags outlet</a>

# August 5, 2011 11:20 PM

Nike Air Max 2011 said:

Easily, the post is really the greatest on this laudable topic. I concur with your conclusions and will thirstily look forward to your future updates.<a href="www.2011nikeairmaxsale.com/" title="Nike Air Max 2011">Nike Air Max 2011</a>

# August 8, 2011 3:38 AM

cheap Chanel bags said:

Thanks for the Blog, thanks for helping me with your useful Article. I think it is really a great topic to write about on my blog.

# August 8, 2011 11:39 PM

The North Face Canada said:

Thanks very much for this wonderful blog;this is the kind of thing that keeps me going through the day.

# August 9, 2011 3:30 AM

LV Trolley Bags said:

Bravo! I agree with your Blog and I will be back to check it more in the future .so please keep up your work. I love your content & the way that you write. It looks like you have been doing this for a while now, how long have you been blogging for? I hope you will like my Blog too,Please See here <a href="www.hellohandbags.com/" title="Louis Vuitton Trolley Bags">Louis Vuitton Trolley Bags</a>

# August 9, 2011 10:29 AM

wholesale jerseys said:

Just want to say your article is brilliant. The clarity in your post is simply impressive and i can assume you are an expert on this subject.

# August 9, 2011 11:05 PM

supra tk society said:

Your site has helped me a lot to bring back more confidence in myself. Thanks! Ive recommended it to my friends as well.[url=www.supratksocietysale.com]supra tk society[/url]

# August 11, 2011 8:12 PM

Prada handbags 2011 said:

I am extremely impressed thanks for sharing all information. It is a great post for the people to get the proper information.<a href="www.pradahandbags2011.com/" title="Prada handbags 2011">Prada handbags 2011</a>

# August 13, 2011 3:28 AM

Wholesale Caps said:

Interesting posts here.. gracias for sharing so much in your blog.. Greets.

# August 15, 2011 4:58 AM

cheap ed hardy outlet said:

Enjoyed quickly looking over your blog and will bookmark for future use, thanks!

# August 16, 2011 4:32 AM

rtyecript said:

I really liked the article, and the very cool blog

# August 24, 2011 1:51 PM

cheap oakley sunglasses said:

This is a great blog,I like this post it is very good and informative. I am sure that this post will be very much helpful for people. usbonlinegroup

# August 26, 2011 10:03 PM

cheap Chanel bags said:

You guys keep posting awesome website, nice to read some view points from someone who obviously knows the industry.

# September 4, 2011 10:26 PM

baseball caps said:

Thanks for taking this opportunity to converse about  this, I feel strongly  about this and I enjoy learning about this subject.

# September 9, 2011 3:42 AM

cheap herve leger dress said:

I really loved reading your blog. It was very well authored and easy to undertand.

# September 14, 2011 9:48 PM

Abercrombie outlet sale said:

Just want to say your article is brilliant. The clarity in your post is simply impressive and i can assume you are an expert on this subject.

# September 15, 2011 9:17 PM

belstaff uk said:

I agree with your Blog and I will be back to check it more in the future so please keep up your work. I love your content & the way that you write. It looks like you’ve been doing this for a while now, how long have you been blogging for?

# September 27, 2011 11:35 PM

Buy cheap OEM software online said:

T1nC5Q Hello! Read the pages not for the first day. Yes, the connection speed is not good. How can I subscribe? I would like to read you in the future!...

# October 26, 2011 3:13 PM

y8 said:

Great blog.Thanks for you published great article.

# November 5, 2011 5:25 PM

Seukbgq said:

# November 24, 2011 4:10 AM

barbie said:

You guys keep posting awesome website, nice to read some view points from someone who obviously knows the industry. <a href="http://www.oynatsak.info">barbie</a>

# December 14, 2011 4:26 PM

download microsoft software said:

AhluJ6 Somewhere in the Internet I have already read almost the same selection of information, but anyway thanks!!....

# December 28, 2011 12:20 AM

fae said:

faefaefa

f

ae

fa

# January 23, 2012 11:21 AM

y8 games said:

Interesting posts here.. gracias for sharing so much in your blog.. Greets.

# January 29, 2012 6:13 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)