How to check if string contains substring?
$(document).ready(function() { $('select[id="Engraving"]').change(function() { var str = $('select[id="Engraving"] option:selected').text(); if (str == "Yes (+ $6.95)") { $('.engraving').show(); } else { $('.engraving').hide(); } }); });
if (str.indexOf("Yes") >= 0)
could use search or match for this.
str.search( 'Yes' )
will return the position of the match, or -1 if it isn't found.
var testStr = "This is a test"; if(testStr.contains("test")){ alert("String Found"); }
var str = 'It was a good date'; console.log(str.includes('good')); // shows true console.log(str.includes('Good')); // shows false
To check for a substring, the following approach can be taken
if (mainString.toLowerCase().includes(substringToCheck.toLowerCase())) { // mainString contains substringToCheck }