JavaScript
Article
By Sam Deering

Replace String Characters in JavaScript

By Sam Deering

There are two ways of looking at this one:

  1. Replace characters in a string INSIDE THE JAVASCRIPT CODE
  2. Replace characters in a string INSIDE THE HTML OF THE WEB PAGE

Replace String Inside JavaScript Code

str = str.replace("find","replace")

Replace String Inside HTML of Web Page

Using jquery

var value = $("#text").val()+"";
value.replace(".", ":");
var val = $("#text").val();
$("#text").val(val.replace('.', ':'));

Using JavaScript

var elmOperator = $("#" + elm.attr('id').replace('Include', 'Operator'));
var elmOperator = $("#" + elm.id.replace('Include', 'Operator'));

Alternative Methods

--ADVERTISEMENT--

.replaceWith( newContent )

Using jquery

$('.third').replaceWith($('.first'));

Using JavaScript

v var str="Visit BLOGOOLA!";
document.write(str.replace("BLOGOOLA", "JQUERY4U"));

Replace values in form select options

//For <select multiple="multiple"> elements
$('input:text.items').val(function(index, value) {
 return value + ' ' + this.className;

Replace foriegn characters in HTML

var temp = new String('This fori<gn chars test st>ring... So?????');
document.write(temp + '<br/>');
temp =  temp.replace(/[^a-zA-Z 0-9]+/g,'');
document.write(temp + '<br/>');

Source: http://api.jquery.com/replaceWith/

Recommended
Sponsors
The most important and interesting stories in tech. Straight to your inbox, daily. Get Versioning.
Login or Create Account to Comment
Login Create Account