redshot redshot - 10 months ago 42
HTML Question

How to remove character inside element tag in jQuery?

I am formatting a registration form in WordPress. The last element tag contains a "|". This form cannot be edited directly.

This is the current markup of the forgot password tag:

<p id="nav">
| <a href="?action=lostpassword">Forgot password?</a>

How to remove the "|" inside the id="nav" so that the Forgot password? link will remain.


Easy solution would be to just super simple replace the pipe char with nothing. ;)

$("#nav").html($("#nav").html().replace("|", ""));

As discused (and mentioned by A. Wolff) in another answer, it is even a good idea to do this in a function, to prevent double DOM search of the p#nav element. You can do it like this:

$("#nav").html(function(i, html){
    return html.replace("|", "");