Snippet

Comment obtenir l'élément par valeur d'attribut dans JavaScript

4 réponses de la communauté

document.querySelectorAll('[data-foo="value"]');
//find first element with "someAttr" attribute
document.querySelector('[someAttr]')
//find all elements with "someAttr" attribute
document.querySelectorAll('[someAttr]') 
priceChange(price, prefix, key, id) {
            if (prefix === '+' && parseInt(price) > 0) {
                this.price_print = (parseInt(this.originalPrice) + parseInt(price)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
            } else if (price !== null) {
                this.price_print = parseInt(this.originalPrice).toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
            }
            document.querySelectorAll('[id_attr="attr' + key + '"]').forEach(function (el) {
                el.classList.remove('product__storage__item-active');
            });
            document.getElementById('value_option' + id).classList.add('product__storage__item-active');
        }