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
Related
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>
This question already has answers here:
JavaScript DOM remove element
(4 answers)
Remove element by id
(19 answers)
Closed 8 years ago.
I'm using this widget/snippet:
<div class="tbnet-gadget">
<div id="tbnet-g4">Carregando...</div><a id="tbnet-link" href="http://www.tabeladobrasileirao.net/" target="_blank" class="tbnet-link" title="Tabela do Brasileirão">Tabela do Brasileirão</a>
<script async src="http://gadgetsparablog.com/ws/tabeladobrasileirao/script?funcao=g4&campeonato=serie-a" type="text/javascript"></script>
</div>
This widget forces a link on the bottom of it (Tabela do Brasileirão). If I change the href tag, the widget won't work.
I want to still use this widget, but I'm trying to remove that link from the bottom of it.
I managed to remove the href attribute using document.getElementById("tbnet-link").removeAttribute("href");, but the text "Tabela do Brasileirão" is still showing up.
This is how it looks like on JSFiddle: http://jsfiddle.net/3nhwf6tw/
How can I remove the whole <a id="tbnet-link"...Brasileirão</a> using javascript?
Thanks.
http://jsfiddle.net/3nhwf6tw/#&togetherjs=1DF8EF6xuh
How about just using CSS instead:
#tbnet-link{
display: none !important;
}
JSFiddle
Here is the non-CSS version (which is a bit ridiculous):
You can remove this:
<a id="tbnet-link" href="http://www.tabeladobrasileirao.net/" target="_blank" class="tbnet-link" title="Tabela do Brasileirão">Tabela do Brasileirão</a>
If you add this jQuery and remove the script in your html:
$.getJSON("http://54.207.27.130/ws//tabeladobrasileirao/g4.jsonp?callback=?&campeonato=serie-a&time=None", function(k) {
$("#tbnet-g4").html(k.html.replace(/\<script.*?\<\/script\>/, ""));
});
JSFiddle no-CSS
To remove the element:
var el = document.getElementById("tbnet-link");
el.parentNode.removeChild(el);
To just clear the text:
var el = document.getElementById("tbnet-link");
el.innerHTML = ""
If you're up for jQuery, it's really easy:
$(function(){
$("#tbnet-link").remove();
});
I am talking about a span arranged this way -
<span class="x">
<span class="y"></span> my text
</span>
Is there a way to change this text without wrapping it inside another tag?
Something like this :
document.getElementsByClassName('y')[0].nextSibling.nodeValue = 'some other text';
FIDDLE
Your question has been asked and answered before.
Check this.
The JQuery solution to your question is :
$('.x').get(0).lastChild.nodeValue = " some-text-2";.
But anyhow, here's the full answer:
HTML
<div class='x'>
<span class='y'></span>
some-text-1
JS
$('.x').get(0).lastChild.nodeValue = " some-text-2";
Fiddle.
If you want to change a comment using jQuery, you will need to use .contents() and look at the node-type 8 for comment.
This is one more Jquery based answer apart from the native javascript solution from adeneo.
$(".x").contents().filter(function() {
return this.nodeType == 3;
}).text('blah');
This question already has answers here:
How to empty the message in a text area with jquery?
(8 answers)
jQuery - remove user input text from textarea
(5 answers)
Closed 9 years ago.
I have a comment box (textarea) inside a dialog. If the comment is successfully saved I want to clear the contents of the textarea and close the dialog box. ATM the dialog box will close but I need to wipe its contents.
<textarea id="CommentBox" type="text" runat="server" rows="7"
maxlength="2000" />
if (CommentSuccessfullyUpdated == "TRUE")
{
//empty the comment box??
//something like
$("#CommentBox").empty();
//closes the dialog box
$("#dialog").dialog('close');
Thanks for any replies
Edit:
Thanks for the help guys. It is running through the code but its not working. I think it has to do with in order to pick up the correct vales and resolve a biding issue I had to use:
function SubmitButton() {
var commentBoxData = $('#<%=CommentBox.ClientID%>').val();
}
When run through with a breakpoint returns:
function SubmitButton() {
var commentBoxData = $('#ctl00_ContentPlaceHolder1_CommentBox').val();
}
AND:
<textarea name="ctl00$ContentPlaceHolder1$CommentBox" id="ctl00_ContentPlaceHolder1_CommentBox" type="text" rows="7" maxlength="2000"> </textarea>
So Im guessing im not referencing the same textarea when I try to empty it.
Also tried
$("#CommentBox.ClientID").val('');
but no joy....ay ideas?
$('#CommentBox').val('');
Use the val() method, passing in an empty string.
Documentation: http://api.jquery.com/val
Also, your mark up is wrong. textarea isn't a self-closing element. You need a </textarea> tag. And the type="text" isn't necessary (probably not actually valid either)
As per your edit, you can either set the IDs to be static at the top of your .aspx file (I think it's ClientID="static")
Or you can use a different selector:
$('textarea').filter('[id*=CommentBox]').val('');
You can use val:
$("#CommentBox").val('');
http://api.jquery.com/val/
JSFiddle
http://jsfiddle.net/KhPM6/1/
Edit:
You are not referencing the ASP.NET generated text area correctly. As you have shown in your question, you need to reference it like:
$('#<%=CommentBox.ClientID%>').val('');
$('textarea#CommentBox').val('');
I am looking around on the web but I am not finding anything useful. :(
I am having a problem that I don't understand. I am sure that I am doing something wrong, but I don't know well the syntax of jQuery to understand what is that I am not doing right.
I am using animations with JS and CSS 3, and I am having troubles with empty spaces between the words, and to solve this problems I have to find a way to substitute chars inside a string of text with something else. Like an empty space with a , or as a test that I was trying to do a "n" with a "xxxxx".
What I think that I am doing is:
when the page is loaded
Modify the string of any paragraph with the class .fancy-title that contains "n" with a text "xxxxx"
So:
$(document).ready(function(){
for(i=0; i< myLength+1; i++){
var charCheck = $(".fancy-title").text()[i];
if(charCheck == "n"){
charCheck.replace("n", "xxxxxxxx");
}
}
});
But I receive an error that it said:
charCheck.replace("n", "xxxxxxxx"); it is not a function
I am using jquery
and other scripts that are based on jquery to make animations, rotation and scaling... and they are all in the HEAD with jquery first to load.
What am I doing wrong? Manipulation in jQuery does it need a specific .js extension? I understood that was in the basic capability of jQuery, and looking at other examples all creates me the same kind of error.
I even tried
if(charCheck == "n"){
$(".fancy-title").text()[i] == " "
}
But simply the modification it is not applied on the page. I tried with innerHTML as well :(
I feel so incompetent...
Thank you in advance for any help.
$(document).ready(function(){
$(".fancy-title").each(function(i){
var text = $(this).html();
$(this).html(text.replace(/ /g, " "));
})
});
You have no problem with the replace part :).
$(document).ready(function(){
$(".fancy-title").each(function () { //for all the elements with class 'fancy-title'
var s=$(this).text(); //get text
$(this).text(s.replace(/n/g, 'xxxxxxxx')); //set text to the replaced version
});
});
Just a quick example, hope it works.
Have you tried the css style white-space:pre instead of replacing ' ' with ' '?
http://de.selfhtml.org/css/eigenschaften/ausrichtung.htm#white_space
It seems you are trying to replace a single character with a new string.
You might be able to get the right result by dropping the iteration and simply call .replace on the jQuery-object.