Replace just the text on a web page via Jquery [duplicate] - javascript

This question already has answers here:
How do I select text nodes with jQuery?
(12 answers)
Closed 7 years ago.
Using Tampermonkey I want to reverse the text on the bbc news website (why? because), obviously this is locally only. I have already written something to replace a part of it, the problem is accessing all the text on the page. Is there an efficient way of doing this in JQuery?
e.g. I have a reverse() function, given:
<div>
Text1
<span class="...">Text2</a>
<fieldset>
<legend>Text3</legend>
Is there some way of targetting Text1, Text2 and Text3 without touching anything else? I can write something to explicitly check the tagName while traversing the DOM and hope there's a text type, or something similar, but I'm wondering if JQuery already has something for doing this?
thanks

Reverse away!
$('*').contents().filter(function() {
return this.nodeType === 3;
}).each(function() {
this.nodeValue = esrever.reverse(this.nodeValue);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://raw.githubusercontent.com/mathiasbynens/esrever/master/src/esrever.js"></script>
<div>
Text1
<span class="...">Text2</a>
<fieldset>
<legend>Text3</legend>

Related

get epoch time from span [duplicate]

This question already has answers here:
How to get value of a div using javascript
(7 answers)
Closed 3 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I had this problem when I get the value from a div:
function sync(){
var n1 = document.getElementById('editor').value;
alert(n1);
var n2 = document.getElementById('news');
n2.value = n1;
}
div with id editor looks like this:
<div class='message' id='editor' contenteditable="true" onkeyUp='sync()' style="color: black"></div>
When I put something in that div it will alert me undefined and that will also come in the textarea i paste it in too. So the problem is obviously by this:
var n1 = document.getElementById('editor').value;
What am I doing wrong?
Try this
var n1 = document.getElementById('editor').innerHTML; // or innerText, or textContent
I think it's important to note that even if <div> was a HTMLInputElement you would still keep getting undefined because your div,
<div class='message' id='editor' contenteditable="true" onkeyUp='sync()' style="color: black"></div>
Has no value attribute, here is an example of a div with a value attribute:
<div class='message' id='editor' value='hello'></div>
However, as mentioned in other answers, even though you have entered a value it, .value will still return undefined because <div> is a HTML element and not a HTMLInputElement.
If you really need to store some information in the value of the div you can always do something like this:
<div id="mydiv"></div>
<script>document.getElementById('mydiv').value='hello';</script>
Right after the div loads, you force 'hello' as the value.
The only reason you'd do this is if you really want to store data within the div's value and can't store it within the innerHTML because the div is visible.
If you want to store the information within your div like this:
<div id="editor">all the information i want to store</div>
Then document.getElementById('editor').innerHTML; is the correct solution, but remember that users will be able to see the information on the webpage.
HTML Elements and HTMLInputElements are different things.
The value attribute is present in HTMLInputElement in HTMLInputElement Reference
div elements are HTMLElement
If you change your div to input your Javascript will work fine.
The option is to use innerHTML as said in another answer but be aware that this could be HTML fragment at times if you don't control the HTML or over time forget the Javascript and can lead to defects as things.
FYI plain elements attribute reference(there is no value)

What is the difference between using multiple append function and single append function [duplicate]

This question already has an answer here:
Prepend divs without closing them
(1 answer)
Closed 7 years ago.
I am very confused how the append function works. After I tried using append 3 times, It shows very different from single append function.
In javascript (Append 1 time)
var divStep = $("#stepTab");
divStep.append('<div style="width:400px;height:40px;float:left;clear:left;"><input type="checkbox" />Test</div>');
In javascript (Append 3 times)
var divStep = $("#stepTab");
divStep.append('<div style="width:400px;height:40px;float:left;clear:left;">');
divStep.append('<input type="checkbox" />Test');
divStep.apend('</div>');
In html
<div id="stepTab">
</div>
I found that the append 1 time method worked well as I expected. The another one is very baffling.
Please can someone explain me a little bit about the difference of these 2 methods? I tried to search on the internet but I can't even know a keyword for the search.
I'm very poor in English. So sorry if it is an ambiguous question.
Thank you in advance.
Yea... Let's see what happened every time you append something to the element.
var divStep = $("#stepTab");
divStep.append('<div style="width:400px;height:40px;float:left;clear:left;"><input type="checkbox" />Test</div>');
console.log(divStep.html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="stepTab"></div>
return <div style="width:400px;height:40px;float:left;clear:left;"><input type="checkbox">Test</div>
var divStep = $("#stepTab");
divStep.append('<div style="width:400px;height:40px;float:left;clear:left;">');
console.log(divStep.html());
divStep.append('<input type="checkbox" />Test');
console.log(divStep.html());
divStep.append('</div>');
console.log(divStep.html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="stepTab"></div>
it returns
#1 <div style="width:400px;height:40px;float:left;clear:left;"></div>
add <div...left;"> and </div> automatically.
#2 <div style="width:400px;height:40px;float:left;clear:left;"></div><input type="checkbox">Test
#3 <div style="width:400px;height:40px;float:left;clear:left;"></div><input type="checkbox">Test
nothing added.
Conclusion: I suppose that the string will be automatically turned into a complete html element/object, instead of appending the html string directly to the innerHTML of the parent node. Because </div> can't be turned into a html element since it is not a fully enclosed HTML, so nothing happened in #3 append.
.appennd is not a native javascript function. its a jQuery method.
The append() method inserts specified content at the end of the selected elements.
In your code append 3 won't work as you expected since it is not a fully enclosed HTML, but 3 sibling dom elements.

Get value from div with javascript: Always get undefined [duplicate]

This question already has answers here:
How to get value of a div using javascript
(7 answers)
Closed 3 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I had this problem when I get the value from a div:
function sync(){
var n1 = document.getElementById('editor').value;
alert(n1);
var n2 = document.getElementById('news');
n2.value = n1;
}
div with id editor looks like this:
<div class='message' id='editor' contenteditable="true" onkeyUp='sync()' style="color: black"></div>
When I put something in that div it will alert me undefined and that will also come in the textarea i paste it in too. So the problem is obviously by this:
var n1 = document.getElementById('editor').value;
What am I doing wrong?
Try this
var n1 = document.getElementById('editor').innerHTML; // or innerText, or textContent
I think it's important to note that even if <div> was a HTMLInputElement you would still keep getting undefined because your div,
<div class='message' id='editor' contenteditable="true" onkeyUp='sync()' style="color: black"></div>
Has no value attribute, here is an example of a div with a value attribute:
<div class='message' id='editor' value='hello'></div>
However, as mentioned in other answers, even though you have entered a value it, .value will still return undefined because <div> is a HTML element and not a HTMLInputElement.
If you really need to store some information in the value of the div you can always do something like this:
<div id="mydiv"></div>
<script>document.getElementById('mydiv').value='hello';</script>
Right after the div loads, you force 'hello' as the value.
The only reason you'd do this is if you really want to store data within the div's value and can't store it within the innerHTML because the div is visible.
If you want to store the information within your div like this:
<div id="editor">all the information i want to store</div>
Then document.getElementById('editor').innerHTML; is the correct solution, but remember that users will be able to see the information on the webpage.
HTML Elements and HTMLInputElements are different things.
The value attribute is present in HTMLInputElement in HTMLInputElement Reference
div elements are HTMLElement
If you change your div to input your Javascript will work fine.
The option is to use innerHTML as said in another answer but be aware that this could be HTML fragment at times if you don't control the HTML or over time forget the Javascript and can lead to defects as things.
FYI plain elements attribute reference(there is no value)

How to use document to rewrite a paragraph [duplicate]

This question already has answers here:
Change content of a div using JavaScript
(4 answers)
Closed 9 years ago.
document.getElementById("txtOutput").value = result;
instead of using .value=result, can I write something else to say, rewrite to a specific div or paragraph?
If you want to write something (including html-formatations) you can use innerHTML orherwise if you need only the text use innerText:
innerHTML
document.getElementById("txtOutput").innerHTML = result
innerText
document.getElementById("txtOutput").innerText = result;
Yes you can. Look at this simple JsFiddle for an example.
HTML:
<div id="me">
<span>old stuff</span>
</div>
Script
document.getElementById('me').innerHTML = "new stuff";
you can use innerHTML:
document.getElementById("txtOutput").innerHTML = 'some content';

JQuery: select paragraphs that only contain " " [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove elements with only a space using jQuery
Wordpress does this thing with adding <p> </p> when doing a line-break in the backend.
I wonder if it is possible to select those p's somehow with jquery.
I guess empty() wouldn't work since it isn't really empty but contains a . But contains() wouldn't probably work either since a lot of other paragraphs also contain a space, right?
I only want to select those paragpahs that have only this " " inside.
UPDATE
I should have mentioned that I don't want to remove the ps but add a class to it.
Add this to your functions.php file if you have access to the theme files.
function user_content_replace($content) {
return str_replace('<p> </p>','<p class="example"> </p>',$content);
}
add_filter('the_content','user_content_replace', 99);
I made it insert a class instead of removing.
This would work to select the paragraphs in question:
$("p").filter(function() {
return $.trim($(this).html()) == ' ';
});
You can do something like this
$(document).ready(function(){
$("p").each(function(){
if($(this).html()==" ") {
alert($(this).attr('id'));
}
});
});
And the HTML I used was
<p id="test" class="les"> </p>
<p class="les" id="test1">aaaa </p>
So it should alert only p with id test
Hope this helps

Categories

Resources