I created code to check a particular value in the input search box. If it is, the web will focus on the searched element.
My html
<input type="search" id="search-box" placeholder="Search">
<button type="submit" id="searchbar" onclick="Focus()"</button>Search</button>
My JavaScript
function Focus(){
var search = document.getElementById("search-box").value;
if(search == "about"){
document.getElementById("about").focus();
}
else{
alert("Not Found");
}
}
This is working, but if I change to this
if(search == "about" || “About”)
Then the else statement is not working.
Is there any other method to make about case sensitive Like About, AbOut, aBoUT, etc all are equal
CodePudding user response:
You can always do:
const toCompare = 'AboUT';
if(toCompare.toLowerCase() === 'about'){
// --
}else{
// --
}
CodePudding user response:
You can use the if statement as below.
if (search == "about" || search == "About"){}
But as you mentioned, there can be many combinations of capitals and simples. So you should convert all the text to simple (convert to a standard format) and do the check like below.
if (search.toLowerCase() == "about"){}
CodePudding user response:
if(search == "about" || “About”)
will always evaluate true as "About" is a non empty string and is Truthy.
You need to write if(search == "about" || search == “About”)
Alternatively, a better syntax is to not make it case-sensitive, and write
if(search.toLowerCase() == "about")
CodePudding user response:
You could use search.toLowerCase() == "about"
to see if it is a case insensitive match. Make sure you're using the right quotation marks. As you've used curly quotes in some instances.