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

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().

Related

select text inside a paragraph tag [duplicate]

This question already has answers here:
How can I use the event #select to select some text from a paragraph in Vue.js?
(2 answers)
Closed 2 years ago.
Is it possible to select text or get the position of highlighted text inside a paragraph <p> ?
I am showing a text sentence by sentence using a loop of paragraph in vuejs.
<p
class="mreadonly text-left mark-context"
v-for="line in jsonData.first.segments"
v-bind:key="line.idx"
#mouseup="mouseupSel($event)"
>{{line.text}}</p>
The mouseup is what I think is the only possible event I can use, since onselect (#select on vue) doesn't work with paragraph.
What could I do to get an onselect-like event, taking into account that I need the selected text mainly to split the original complete string in two parts?
Should I convert my paragraph into input tags? Is it possible to make them look like paragraphs?
Thanks to Leeish, here is what I've done. It's a slightly duplicate question apparently, but the answer here might help anyway.
The HTML stay the same, with #mouseup.
On methods I wrote (as quick and dirty example):
mouseup(ev){
console.log('onselect',ev);
var gs = window.getSelection();
var data = gs.baseNode.data;
var data2 = data.substr(gs.anchorOffset+gs.toString().length,data.length);
var data1 = data.substr(0,gs.anchorOffset) + gs.toString();
console.log(data1);
console.log(data2);
},

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 change position of div element using javascript [duplicate]

This question already has answers here:
How to swap DOM child nodes in JavaScript?
(11 answers)
Closed 5 years ago.
Firstly, I don't know if this title is correctly and tell what I really want. I have html form with many elements ex. div, p or js. For example:
<div id="main">
<div id="abc1"></div>
<div id="abc2"></div>
<style>.module a {min-height: 25px;padding-left: 95px;}</style>
<form id="myForm">...</form>
<script>...</script>
</div>
I want to change order or two elements: abc1 and abc2. In js I have:
$(document).ready(function(){
$('#main').each(function (index, element) {
var html = '';
html += '<div id="abc2">'+ $(element).find('#abc2').html() +'</div>';
html += '<div id="abc1">'+ $(element).find('#abc1').html() +'</div>';
$(element).html(html);
});
});
This works. But the problem is that I need to include each element from div#main. It is possible to include other without write each of one in javascript ?
Kind regards
To me it looks a little costly operation, each time you search for an element and search it takes your frontend bandwidth. I wonder if you have tried fixing it with css and flex boxes? If you are using Bootstrap, there also you have classes which can work in your case.

JS how right replace element? [duplicate]

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.

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