• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

jquery select all labels in form

hi there,

(i am using jquery-1.3.2.min.js and mvc)

I have several labels and textboxes in my page.  
50% of them have the word "Sdc" in the id  - i.e "SdcStartLabel" "SdcEndLabel" "SdcTextbox" etc etc

After the page has loaded, if a certain value is false - i want to disable all the labels and textboxes  that have "Sdc" in their id.

When i say disable - for the labels - i want to make the back ground grey - and put "N\A" and for the text inputs - to actually disable them.

Is this possible to do in one statement - rather than explicitly call every label and text box.

If so could someone show me how.


Once the
  • 2
1 Solution

var trlist = $('tbl_registration').getElementsByTagName('label');
            var arrTr = $A(trlist).findAll(function (node){return node.id.match("^Sdc")});
                  node.style.display = action ;

u can change label and $('tbl_registration') to ur requirement

where action = block or none .
which would do show and hide

u can even pass these parameter thru ur function so that only one function can be used to both label and ur textbox or checkbox and anything else
Justin MathewsCommented:
Try this (replace variable cond with your condition):
.disable { 
border:1px solid green;
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

var cond = false; //your conditon

$(document).ready(function () {
 if (!cond)
	$('*[id^=Sdc]').each(function (){
		if ($(this).attr("tagname")=="LABEL")
			$(this).attr("className", "disable");
		if ($(this).attr("tagname")=="INPUT")
			$(this).attr("disabled", true);


Open in new window

MrKevorkianAuthor Commented:
im going to go for this one, as it does the wildcarding which is what i hoping for

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now