0

I have a working jQuery codes that have a condition to return true if it matches the name.

jQuery(function(j) {
    var strings = j(".user-nicename").text();  
    if (strings === "name1" ) {
       j('.mention-name').hide();  
       j('.separator').hide();
       j('.separator').hide();  
       j('.field_8263').css("display", "none");
       j('#activity-personal-li').css("display", "none");  
       j('#groups-personal-li').css("display", "none");  
  
    }
});
jQuery(function(j) {
    var strings = j(".user-nicename").text();  
    if (strings === "name2" ) {
       j('.mention-name').hide();  
       j('.separator').hide();
       j('.separator').hide();  
       j('.field_8263').css("display", "none");
       j('#activity-personal-li').css("display", "none");  
       j('#groups-personal-li').css("display", "none");  
  
    }
});

However, I would like to add these names in an array. But once I do that, the code is not working anymore...

jQuery(function($) {
    var strings = $(".user-nicename").text(); 
    var authorName = ["name1" , "name2"];
    if (strings === "authorName" ) {
       $('.mention-name').hide();  
       $('.separator').hide();
       $('.separator').hide();  
       $('.field_8263').css("display", "none");
       $('#activity-personal-li').css("display", "none");  
       $('#groups-personal-li').css("display", "none");  
  
    }
});

Maybe anyone can help me? I think the "array" code is not correct. Thank you!

1 Answers1

1

What I think you are trying to do is add all the string into the array and then see if the string exists in the array, to do that you want

var strings = $(".user-nicename").text(); 
var authorName = ["name1" , "name2"];
if (authorName.includes(strings)) {
    //Then run your code
}
Da Mahdi03
  • 1,468
  • 1
  • 9
  • 18