5

How to write $('label.someClass').attr('valid', true); in Java Script with out using jQuery?

2
  • 1
    There is no valid attribute for <label> elements. Commented Feb 2, 2012 at 16:56
  • The selector part "$('label.someclass')" is kind of tricky, can you give the element an ID? Or are you trying to apply this attribute to all elements with a specified class? htmlcssjavascript.com/web/… this link could help you. Commented Feb 2, 2012 at 16:58

5 Answers 5

4
Array.prototype.forEach.call(document.querySelectorAll('label.someClass'), function( label ) {
    label.setAttribute('someAttribute', 'someValue');
});

which implies that the browser is capable of ES5. An alternative for .querySelectorAll could be .getElementsByClassName too here.

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

1 Comment

please specify a reason for downvoting. If I made a mistake, help me and other guys out.
1

Find all the label elements using getElementsByTagName which gives an array of matching elements. Run a loop on this array and check if the class name exists and set the attribute using setAttribute method.

if(document.getElementsByClassName){
}
else{
var labels = document.getElementsByTagName('label');
for(var i = 0;i<labels.length;i++){
    if(new RegExp("\\bsomeClass\\b", "g").test(labels[i].className)){
         labels[i].setAttribute('valid', true); 
    }
}

If the browser supports getElementsByClassName then we can use that conditionally.

if(document.getElementsByClassName){
    var elements = document.getElementsByClassName('someClass');
    for(var i = 0;i<elements.length;i++){
        ifelements[i].tagName.toLowerCase() == 'label'){
            elements[i].setAttribute('valid', true); 
    }
}
else{
    var labels = document.getElementsByTagName('label');
    for(var i = 0;i<labels.length;i++){
        if(new RegExp("\\bsomeClass\\b", "g").test(labels[i].className)){
            labels[i].setAttribute('valid', true); 
    }
}

4 Comments

And you've got a , where you should have a ;
why not simply use getElementsByClassName()?
@Quentin - Fixed both of them.
@ajax333221 - Because it is not supported by all the classes. Though we can check for it and use it accordingly.
1

Using a regular expression to match the class:

var labels = document.getElementsByTagName('label');
for(var i = 0;i<labels.length;i++){
    var classes = labels[i].className;
    if(classes && classes.match(/^(\s*|.*\s+)someClass(\s*|\s+.*)$/)){
         labels[i].setAttribute('valid', true); 
    }
}

Comments

0
document.getElementById("myimage").setAttribute("src","another.gif")

Try this Tutorial

2 Comments

The code in the question doesn't use an id to find the elements it is dealing with.
I don't have id, just a class on label element.;
0

Use the setAttribute() method (doc).

4 Comments

What about finding all the label elements which are members of the someClass class?
How to select label by class in pure Java Script?
In pure JavaScript? You can't. You need a DOM API. (OK, so I hate the term 'pure')
getElementsByClassName though you will want to check to make sure it is avalaible in all browser versions. You could have to loop through each and check to see if the type if a lable. OR you can use getelementsbytagname and loop through them to find the class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.