Recently, I've been researching ways to check if a certain value exists in an array using indexOf('textvalue'). However, I have encountered the issue of my 'textvalue' being a substring, which causes no matches to be found. The problem lies in passing the field name to the method and needing to remove specific text by matching the array item based on "Door". Can anyone suggest a solution for this dilemma?
Below is the sample code I am currently working with:
(spnSelectedDisclosures contains a list of selected fields, and the following code snippet aims to eliminate the previous field selection from the text delimited by semicolons.)
var currentText = $("#spnSelectedDisclosures").text();
var existingArr = currentText.split(";")
for (i=0;i < existingArr.length;i++) {
var indexItem = " " + existingArr[i].toString(); // I'm confused as to why indexItem remains an object instead of a string. Adding a space was an attempt to convert it into a string variable.
if (indexItem.contains(substringText)) { // The error occurs here, stating that the object does not have a 'contains' method.
alert("A match has been found at position: " + i);
textToRemoveIndex = i;
break;
}
}
var textToRemove = existingArr[textToRemoveIndex];
var newText = currentText.replace(textToRemove,"");
$("#spnSelectedDisclosures").text(newText);