0

Just a simple question i cant seem to figure out. I have a simple function where i can click on the item[i].name and it opens up a description of the item. But i want it as when i click on the item[i].name or the item[i].cost, the description will open. But i can make only one of them work, i cant make it work with both.

Ive tried

if (item[i].name, item[i].cost == event.target.textContent) {

and a few other things but i cant seem to make it work with both. Is there a short way im just missing out on?

         el.addEventListener('click',function(event){
             $("#list").empty();
             console.log(event.target.textContent);
             for (var i = 0; i < item.length; i++) {
                     if (item[i].name == event.target.textContent) {
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
James
  • 35
  • 7
  • **`&&`**: logical and, **`||`**: logical or. – Angel Politis May 21 '19 at 03:49
  • 2
    Possible duplicate of [Check variable equality against a list of values](https://stackoverflow.com/questions/4728144/check-variable-equality-against-a-list-of-values) – VLAZ May 21 '19 at 03:50

3 Answers3

4

you should do like below:

if (item[i].name == event.target.textContent || item[i].cost == event.target.textContent){
   // your code
}

Note: There are multiple ways to do it more efficiently.

Shivang Agarwal
  • 1,825
  • 1
  • 14
  • 19
0

Use a logical OR operator ||:

if (item[i].name == event.target.textContent || item[i].cost == event.target.textContent) {...}

Or use includes:

if ([item[i].name, item[i].cost].includes(event.target.textContent)) {...}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0
    if(document.getElementById("name_input").value == "" && document.getElementById("exampleInputEmail1").value == "" && document.getElementById("message_input").value == "" && flag4==0)
    {        
        document.getElementById("submit").disabled = true;
    }
    else if(document.getElementById("name_input").value != "" && document.getElementById("exampleInputEmail1").value != "" && document.getElementById("message_input").value != "" && flag1 == 1 && flag2 == 1 && flag3 == 1 && flag4==1) 
    {
        document.getElementById("submit").disabled = false;
    }```

 - It's just an example from one of my projects. you can take reference
   from these multiple if conditions in one if block.