The code in the attached link displays all values associated with the element className “oddsType”.
How do I re-write the code to display only the numerical values associated with the “X” title on the web page.https://drive.google.com/file/d/1JAIjdg0z9Q_VcuzrsfcvqqhRBMHEmigN/view?usp=sharing
please don’t give code screenshots, it’s impossible to debug from one
please provide a live version of your project (on for example repl.it, codepen etc)
You need to add check for ‘X’ in the loop:
for (let i = 0; i < b.length; i++) {
if (b[i].innerText === 'X') {
console.log(b[i].nextElementSibling.innerText)
}
}
I tried using your code example to extract some values together on the web page. The code only extracted the ‘12’ values while it ignored the “Event ng-binding”(variable a) element values and “X” values.
Here’s the code:
a=document.getElementsByClassName(“Event ng-binding”), b=document.getElementsByClassName(“oddsType”)
for(i=0; i<a.length,i<b.length;i++){
if(b[i].innerText===‘X’,b[i].innerText===‘12’)
{console.log(a.innerText,b[i].nextElementSibling.innerText)}}
In JavaScript if you separate expressions with comma, the return value of the last one will be used.
So this code:
if(b[i].innerText===‘X’,b[i].innerText===‘12’)
works as intended.
Read about Logical OR
So, what should I do to rectify the code. I intend for the values of “a”, “X” and “12” to be returned.
Will try Logical OR and find out. Thanks.