MozillaZine

Javascript question

Discuss how to use and promote Web standards with the Mozilla Gecko engine.
Bethrezen
 
Posts: 405
Joined: September 13th, 2003, 11:56 am

Post Posted August 15th, 2019, 9:21 am

Hi all

I know this is probably a pretty basic question but I'm curious suppose you have a condition like this.

Code: Select all
if(document.querySelector('#value1:not(:checked)')
   && document.querySelector('.value1 input:checked')
   || document.querySelector('#value2:not(:checked)')
   && document.querySelector('.value2 input:checked')
   || document.querySelector('#value3:not(:checked)')
   && document.querySelector('.value3 input:checked'))
{
    //do something
}


Is there a way to shorten that down a bit ? After a bit of searching about the simplest way I found was to do

Code: Select all
var values = ["value1","value2","value3"];

for (var i = 0; i < values.length; i++)
{
  if(document.querySelector('#' + values[i] + ':not(:checked)')
     && document.querySelector('.' + values[i] + ' input:checked'))
  {
    // do somthing
  }
}


but I'm wondering if there is a better / simpler way to look for multiple values without having to do

Code: Select all
document.querySelector('#value1:not(:checked))
document.querySelector('#value2:not(:checked))
document.querySelector('#value3:not(:checked))

document.querySelector('.value1 input:checked')
document.querySelector('.value2 input:checked')
document.querySelector('.value3 input:checked')


maybe something like

Code: Select all
if(document.querySelector('(#value1 || #value2 || #value3):not(:checked)')
&& document.querySelector('(.value1 || .value2 || .value3) input:checked'))
{
    // do something
}

Return to Web Development / Standards Evangelism


Who is online

Users browsing this forum: No registered users and 1 guest