JS how right replace element? [duplicate] - javascript

This question already has answers here:
jQuery if div contains this text, replace that part of the text
(5 answers)
Closed 8 years ago.
...
<div>
<div>
{FirstName} {POSITION}
</div>
</div>
...
We would like find element {FirstName} and repalce him on test.
For this we make:
$("{FirstName}").replaceWith("test");
But it is not working...
Tell me please how right make replace?

What you should do is target the parent DIV (lets pretend it has a class of .test)
var content = $('.test').html();
var new_content = content.replace('{Whatever}', 'Hello');
$('.test').html(new_content);
Or in short (I haven't tested this, but it should work)
$('.test').html($('.test').html().replace('{Whatever}', 'Hello'));

If you just want to replace a string in DOM elements, the question has been asked before :
Replace all strings in dom element
But best to do this would be to use a template engine.

Related

How Can I Use The HTML Button Element In JavaScript Completely [duplicate]

This question already has answers here:
Why is document.write considered a "bad practice"?
(17 answers)
Closed 1 year ago.
Can You help me find out how to use the html button element in JavaScript because
document.write("<body> <div> </div> </body>")
is not working for buttons or drop down menus so can you help me?
my guess is the
.write
Is the problem because that probably means text and a button is not text.
This is not the most correct way to do this. You must create elements and add them to the DOM tree.
let button = document.createElement('button');
document.body.append(button);
button.onclick = () => {
alert('button clicked');
}
I think you want append(). This example is stolen from this MDN page
let parent = document.createElement("div")
let p = document.createElement("p")
parent.append(p)
console.log(parent.childNodes) // NodeList [ <p> ]

Convert HTML text into plain text [duplicate]

This question already has answers here:
How to get the pure text without HTML element using JavaScript?
(10 answers)
Closed 5 years ago.
Is there a method or function that does this, or do I have to check each character of a string. To give an idea on what I'm talking about, for example. I have an HTML TEXT like:
<p><strong>Welcome </strong>to this <em>message, </em><span style="text-decoration: underline;">you may come in peace.</span></p>
I need to convert it into a plain text, resulting into:
Welcome to this message, you may come in peace.
The text will come from a textarea which is a child of a div with an id = editor-email.
I also wanted to take the current text, but it won't work.
var textEmail = $('#editor-email').find('textarea').text();
You can do it like with pure JS
let a = `<p><strong>Welcome </strong>to this <em>message, </em><span style="text-decoration: underline;">you may come in peace.</span></p>
`;
let d = document.createElement('div');
d.innerHTML = a;
console.log(d.innerText);
If you're looking for a JQuery-free solution, you can select the element and use .innerText:
document.querySelector('#myElement').innerText
I think you need:
$("p").text()
EDIT:
If you want the value from the textarea then you will need:
$("#editor-email > textarea").val();

How to create div with text using jQuery? [duplicate]

This question already has answers here:
How to put HTML in jQuery .text()
(6 answers)
Closed 5 years ago.
I am trying to grab text inside a page, and put it inside a div/span element using jQuery.
For example, grab <p>Did you know "LARC" is a Medical Term?</p> off a webpage and put it inside of a div like so: <p>Did you know <div class="LARC IS COOL">"LARC"</div> is a Medical Term?</p>
The reason I am asking is because I need to run a function on a specific word with a class but I don't have access to the html where that word is due to the fact it comes from an outside source and is loaded on the page.
This is what I currently have:
jQuery('.gfield_label').each(function() {
var text = jQuery(this).text();
jQuery(this).text(text.replace("LARC", "<div class='LARC IS COOL'>LARC</div>"));
});
But it just outputs this on the webpage:
Did you know <div title='THIS BETTER WORK'>LARC</div> is a medical term?
You will want to use the HTML function when replacing.
see: http://api.jquery.com/text/ and
http://api.jquery.com/html/
jQuery('.gfield_label').each(function() {
var text = jQuery(this).text();
jQuery(this).html(text.replace("LARC", "<div class='LARC IS COOL'>LARC</div>"));
});
Instead of using .text(), try using .html().

How to create a "bbcode" javascript with replace [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 9 years ago.
Markup:
<div class="post">
[tag]dfghf[/tag][tag]gh[/tag][tag]cxbcvb[/tag][tag]gh[/tag][tag]cxbcvb[/tag][tag]gh[/tag]
</div>
JS I have so far-
for(var j=0;j<post.length;j++){
var postText = $(post[j]);
postText.html(postText.html()
.replace('[tag]','<span class="tag">')
.replace('[/tag]','</span>')
);
}
Though this seems to only replace the very first tag. How do I get it to replace all the tags?
Try
postText.html(postText
.html()
.replace(/\[tag\]/g,'<span class="tag">')
.replace(/\[\/tag\]/g,'</span>')
);
Ex:
$('.post').html(function(){
return $(this)
.html()
.replace(/\[tag\]/g,'<span class="tag">')
.replace(/\[\/tag\]/g,'</span>')
})
Demo: Fiddle

how to get the text inside the textarea using jquery and display on the html? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
jQuery get textarea text
So i have a text area that takes the users input. How do i get text inside the text area and put it in my <div id="prev"> inside <p> tag?
i tried using this:
function displayCustom() {
var customText = $('textarea#custom').val();
$('prev p').text(customText); //<-- this doesnt work
//alert(text); <-- if i try using this it works
}
You should use # for the ID selector:
$('#prev p').text(customText);
You can append your text inside <p> like this:
var customText = $('#custom').val();
$('#prev').append('<p>'+customText+'</p>');

Categories