Remove only text from innerHTML [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have dynamically generated HTML, for example:
const content = '<b>some <i>text</i><b>'
How can I get this content and remove last 2 letters, to get <b>some <i>te</i><b>? I'm getting this content via el.innerHTML. The main idea, I want to keep html how it is, and crop only text.

Using regex on HTML is... problematic.
An alternative method would be to create a new element, set its innerHTML to the string. Then update the text content of the i element.
const content = '<b>some <i>text</i><b>';
const span = document.createElement('span');
span.innerHTML = content;
const i = span.querySelector('i');
i.textContent = i.textContent.substring(0, i.textContent.length - 2);
console.log(span.innerHTML);

Related

Change the H1 tag to H2 using Javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am looking for a way in Vanilla JavaScript to change the <h1> tag to <h3> tag for this HTML code:
<h1 class="price-heading ult-responsive cust-headformat" data-ultimate-target="#price-table-wrap-3686 .cust-headformat" data-responsive-json-new="{"font-size":"","line-height":""}" style="font-family:'Roboto';font-weight:bold;">Basic</h1>
How can I achieve this?
var el = document.getElementsByTagName('h1')[0];
var dummy = document.createElement('h2');
dummy.innerHTML = el.innerHTML;
el.parentNode.replaceChild(dummy, el);
https://jsfiddle.net/Lzg47mek/

How to Encode only content inside of html tags in Javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How do I encode in JS only what's inside tags but I don't know the order in which the tags will appear. I do know that I will only have em, strong, br and a tags tho. Look at the funny o and & below. Thanks.
<div>hello my <em>friend</em>, this is how you <em><strong>search</strong></em> <strong>göogle</strong> & remember, don't lose your <strong><em>googliness</em></strong>...</div>
So it should encode only the special characters inside the tags but not the tags themselves.
Desired Output:
<div>hello my <em>friend</em>, this is how you <em><strong>search</strong></em> <strong>göogle</strong> & remember, don't lose your <strong><em>googliness</em></strong>...</div>
This should help you get up & running. Simply iterate over the childNodes and modify their textContent property:
[...div.childNodes].forEach(
(el) => {
// here you can do whatever you want with your nodes
el.textContent = el.textContent.toUpperCase();
// so this is the place to implement your encoding logic
}
)
<div id="div">
hello my <em>friend</em>, this is how you <em><strong>search</strong></em> <strong>göogle</strong> & remember, don't lose your <strong><em>googliness</em></strong>...
</div>

javascript - clone DOM body without script tags [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am developing a chrome extension in which I need to clone the body tag-childrens. (a clone - so that the elements does not get updated when the DOM changes). And Re-append them to the body tag, without the script tags.
I am doing this to avoid the script tags for executing.
The problem is that I don't want script tags cloned along with the other elements in the body. I read that using regex to filter/search for stuff in the html is general not a good idea.
How can I accomplish my goals, what are my options?
A vanilla javascript approach might look like the following.
var div = document.createElement('div');
div.innerHTML = document.body.innerHTML;
div.querySelectorAll('script').forEach(function(scriptTag) {
scriptTag.parentNode.removeChild(scriptTag);
});
document.body.innerHTML = div.innerHTML;
<div>
A div
</div>
<script>
console.log('hi');
</script>
<p>
A paragraph
</p>
Using jQuery you could try something like:
var $clone = $('<div>').append($('body').html())
$clone.find('script').remove()
Then when you want to replace:
$('body').html($clone.html())
Note that all event listeners will be lost with this approach

get webpage html element values without opening the page in new window or new tab using JavaScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Just like the title says I need a way to get the HTML of a webpage with out opening it in a new tab or window. I am making a chrome extension that will take element values from one page and append them to another page.
var htmlObject = gethtml("url");
would like some thing like this
You can use jQuery to get the content of the webpage:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
var htmlObject;
var yourURL = 'url.html';
$.get(yourURL, function(html) {
htmlObject = html;
});

Output div id data into javascript variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Got this div id with html data, how can i output that html data into a javascript variable? example
var url = '(HtmlDivId)'; is this possible?
Are you are trying to read content of the div?
//jQuery
var url = $('#HtmlDivId').html();
//javascript
var url = document.getElementById('HtmlDivId').innerHTML;
Sure
var url = document.getElementById('html-div-id').innerHTML;

Categories

Resources