Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get element by tag and class

Tags:

javascript

I have a script

var firstImg = row.getElementsByTagName('img')[0];

and later

if (x){ firstImg.src='/images/checked.png'; }

I'd like to define that the img should be of class='something'

(Get first img with class='something')

like image 313
Martin Avatar asked Oct 02 '12 07:10

Martin


People also ask

Is there a get element by class?

The getElementsByClassName() method returns an array-like of objects of the child elements with a specified class name. The getElementsByClassName() method is available on the document element or any other elements. The method returns the elements which is a live HTMLCollection of the matches elements.

Can you get element by class in JavaScript?

The JavaScript getElementsByClassName is used to get all the elements that belong to a particular class. When the JavaScript get element by class name method is called on the document object, it searches the complete document, including the root nodes, and returns an array containing all the elements.

What is get element by tagname?

Definition and Usage The getElementsByTagName() method returns a collection of child elements with a given tag name. The getElementsByTagName() method returns a NodeList object.

How do you get one element from a class?

If you want only the first element in the DOM with that class, you can select the first element out of the array returned. var elements = document. getElementsByClassName('className'); var requiredElement = elements[0]; Else, if you really want to select only one element.


4 Answers

Use the

 querySelectorAll('img.classname')[0]

this returns first image with class set to class name. However jQuery is better!!

$('img.classname')
like image 93
geekman Avatar answered Oct 03 '22 15:10

geekman


Just set it...

firstImg.className += "something";

...or...

firstImg.classList.add("something");

If you can get away with not supporting older browsers.

Further Reading (disclaimer: link to my own blog).

If you're intending to get elements with a certain class name, you can use...

document.getElementsByClassName("something");

...or...

document.querySelectorAll(".something");

Keep in mind getElementsByClassName() isn't in <= IE8.

You can use...

var getElementsByClassName(nodeList, className) {
    var i, results = [];
    for (i = 0; i < nodeList.length; i++) {
        if ((" " + nodeList[i].className + " ").indexOf(" " + className + " ") > -1) {
             results.push(nodeList[i]);
        }
    }

    return results; 
}

Of course, it's super simple if you're using jQuery...

$(".something");
like image 27
alex Avatar answered Oct 03 '22 15:10

alex


this selects the first img with class='something':

var firstImg = $('img.something')[0];
like image 34
Roman Avatar answered Oct 03 '22 17:10

Roman


If you could not throw away the old browsers, then you need a loop.

var imgs = row.getElementsByTagName('img'),
    some_class = 'something',
    i, first_img;

for (i = 0; i < imgs.length; i++) {
    if ((' ' + imgs[i].className + ' ').indexOf(' ' + some_class + ' ') > -1) {
       first_img = imgs[i];
       break;
    }
}
like image 38
xdazz Avatar answered Oct 03 '22 15:10

xdazz