This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Replace all instances of a pattern with regular expressions in Javascript / jQuery
How can I use jQuery to style /parts/ of all instances of a specific word?
Say I have the code:
<div class="replace">
<i>Blah</i>
<u>Blah</u>
</div>
Now, I want to replace all the < within the div with something else.
If I use $('#div').html().replace('<','somethingElse'); it only replaces the first instance.
How can I replace all the instances?
Thanks
Use regex
"anyString".replace(/</g,'somethingElse');
If you want to replace it inside the div then try this.
$('#div').html($('#div').html().replace(/</g,'somethingElse'));
Related
This question already has answers here:
How can I find elements by text content with jQuery?
(8 answers)
Closed 4 years ago.
how can I remove an element using jquery by the text in the element.
Link1
I dont want to remove the element by class or ID. I want to do something like this:
$('a').html('Link1').remove()
Can you help me with this?
Please try this
jQuery
$('a:contains("Link1")').remove();
Demo fiddle
For more reference please refer this.
This question already has answers here:
How can I select an element with multiple classes in jQuery?
(14 answers)
Closed 6 years ago.
I am using jquery bootpag to display pagination on a page
<div class="textgray showing"></div>
How should I tell jQuery to select this div? I tried the following without but neither of these attempts work
$(".textgray .showing").html("some text");
$(".textgray showing").html("some text");
P.S I am a beginner in frontend so bear with me
The space means the second part is inside the first one (it's called the descendant combinator). Remove it:
$(".textgray.showing").html("some text");
You don't need to put that extra space in between selector. space in selector looks for descendant elements:
$(".textgray.showing").html("some text");
This question already has answers here:
How to change a text with jQuery
(8 answers)
Closed 6 years ago.
I have a javascript code. The code always appends some text to an old one. But I dont want that the text is attached to the old text. Instead, I want that it replaces the old text.
Here is the code line:
$("#werkstatt").append("Text");
How can I do that?
Thank you very much!
You have to use empty() method.
$("#werkstatt").empty().append("Text");
or simply you should use .text() method.
$("#werkstatt").text("Text");
This question already has answers here:
Can't select div with id=":1"
(2 answers)
Closed 7 years ago.
I have an element that has a period in it:
<div id="parent">
<div id="my.element"></div>
</div>
and I want to select it using Prototype. I've tried this:
$$('#parent #my.element');
but it isn't working because it thinks the .element part is a class. Is there a way around this?
FYI, it isn't really an option to rename the element. Unfortunately I'm stuck with this naming scheme as well as only Prototype
Thanks
You can always escape your dot in css, so this should work
$$('#parent #my\\.element');
EDIT: As stated by Oriol and tested, you indeed have to use 2 slashes
This question already has answers here:
Find and replace specific text characters across a document with JS
(14 answers)
Closed 8 years ago.
Can anyone share how I could wrap every asterix on a page within an enclosing div.
So that:
Tasty drinks *
Becomes:
Tasty drinks <span class="disclaimer">*</span>
I've tried a few online examples and can get it to work on almost any character except an asterix.
If you are using jQuery, you could use
$("body").children().each(function() {
$(this).html($(this).html().replace(/\*/g,"<span class='disclaimer'>*</span>"));
});
Here's a fiddle
(Adapted from this question)