Aakash Chakravarthy
Aakash Chakravarthy

Reputation: 10701

jQuery find and replace text in a variable

I have a variable like

var theUrl = 'http://www.google.com/?q=%s';

then i have a input box like

<input type="text" id="searchBox" value="" />

and a button like

<input type="button" id="searchButton" value="Search" />

when the button is clicked, i should get a alert where the %s of the var theUrl should be replaced with the user entered text in the textbox

How to do this ? i think find() function only replaces html elements !

Please help

Upvotes: 2

Views: 6415

Answers (1)

SLaks
SLaks

Reputation: 888223

You need to call the replace function, like this:

$('#searchButton').click(function() { 
    alert(theUrl.replace('%s', $('#searchBox').val());
});

Upvotes: 7

Related Questions