Find and Replace String Methods 2
Video
JavaScript Notes
JavaScript
document.addEventListener('DOMContentLoaded', function( ){
let btn = document.getElementById('btnSearch');
btn.addEventListener('click', doFindAndReplace);
});
function doFundAndReplace(ev){
ev.preventDefault( );
let find = document.getElementById('find').value;
let replace = document.getElementById('replace').value;
let p = document.querySelector('.target');
while( p.textContent.indexOf(find) != - 1 ){
p.textContent = p.textContent.replace(find, replace); // the replace method takes 2 arguments
// only replaces the first instance it finds
}
}
Feedback
Submit and view feedback