Javascript String Replace Example

Replacing strings with javascript

Demo

I'm @little te@pot

Output goes here

Replace Text

Code

<p id="source">I'm @little te@pot</p>
<p id="output">Output goes here</p>
<p><a href="#" onClick="return replaceText()">Replace Text</a></p>
<script type="text/javascript">
function replaceText(){
    sourcetext = document.getElementById('source').innerHTML;


    // The string replace function
    outputtext = sourcetext.replace(/@/gi, "REPLACEMENT");


    document.getElementById('output').innerHTML = outputtext;
    return false;
}
</script>

“ Invent Partners supported my cycling event with a stylish website, intuitive CMS and flexible and responsive ecommerce system. Thanks to their hard work and patience, my event sold out in hours, with riders praising the easy to use event entry system and customer service. Many thanks! ”
Danial, London Edinburgh London

Contact Us

© 2024 Invent Partners Ltd | Supporters of FdA Web Design, Wakefield