Чтобы сравнить значение currency
со значением «AUD» в условном выражении, вы хотите выполнить if (currency == "AUD")
(это называется свободное сравнение) или if (currency === "AUD")
(это называется строгое сравнение).
Хотя вам нужно сначала получить .innerText
из currency
. Прямо сейчас currency
- это первый узел , найденный getElementsByClassName
. Поэтому измените его на var currency = document.getElementsByClassName("name")[0].innerText
, чтобы получить текстовое значение узла, а затем вы можете выполнить условие, как указано выше.
function myFunction() {
//check the first 'name' text
var currency = document.getElementsByClassName("name")[0].innerText;
if (currency == "AUD") {
console.log('currency is AUD');
} else {
console.log('currency is not AUD');
}
//check the second 'name' text
var currency2 = document.getElementsByClassName("name")[1].innerText;
if (currency2 == "AUD") {
console.log('currency is AUD');
} else {
console.log('currency is not AUD, it is ' + currency2);
}
}
myFunction();
<span class="name" data-name="">AUD</span>
<span class="name" data-name="">USD</span>