RickyHalim
RickyHalim

Reputation: 295

Jquery replace not working

Hi I am trying to change the contents of a hidden input.

var temp = $("#header").val().replace("(/<\/div><div>/g","<br>");

I tried using the following replace but it does not change it.

May I know what I did wrong? I am trying to replace:

</div><div>

to

<br>

EDIT - this is my program flow

Upvotes: 0

Views: 2340

Answers (2)

Javier del Saz
Javier del Saz

Reputation: 856

To replace all ocurrences in a string, the correct syntax with regExp is:

var temp = $("#header").val().replace(/<\/div><div>/g,"<br>");

You can test it, here:

'</div><div></div><div></div><div></div><div>'.replace(/<\/div><div>/g,"<br>");

returns:

<br><br><br><br>

Upvotes: 1

Gaurang Tandon
Gaurang Tandon

Reputation: 6753

You should do:

.replace(/<\/div><div>/g,"<br>");

The RegExp should not be enclosed within quotes else it would be a String. Also, the .val() method is used for textareas. Use .html() for header, divs, etc.

Upvotes: 0

Related Questions