Removes value from string if present, else adds that value

IndexOf function checks if string contains specific value If/Else statement - Uses Replace method (instead of Remove)

by chayacooper

HTML

<div class="dropdown_box" value="test_case" ></div>

JavaScript

var text_edited = "Bob Smith";
var currentHtml = "Hello this is my uncle Bob Smith";

document.write(currentHtml + "<br/>");

var positionLocation = currentHtml.indexOf(text_edited);
// If wasn't found
if (positionLocation < 1) {         
    (currentHtml = currentHtml + ', ' + text_edited);  
    document.write(currentHtml + "<br/>");
}

// If value found, removes value (removes comma if present) 
else {     
  (currentHtml = currentHtml.replace(text_edited, "")); 
    document.write(currentHtml + "<br/>");    
}