How to find and remove a specific text in a html file?
I already found a code, but I think it doesn't work if there are charkters like "/" or "()" in the HTML-File.
My HTML-Code
<label>Text 1 (Blue/White/Green)</label>
My Script
$("label").children().each(function() {
$(this).html($(this).html().replace(/'(Blue/White/Green)'/g,""));
});
You just need to escape the slashes (and parens) in the regex, as well as remove the apostrophes. Secondarily, you're looking for children that don't exist, as the text is not within a child node, but the content of the label node, so removing the .children() filter will make it work:
$("label").each(function() {
$(this).html($(this).html().replace(/\(Blue\/White\/Green\)/g,""));
});
If you aren't interested in using a regex, you can also use the replace method with a string instead, though as written this will only replace the first instance:
$("label").each(function() {
$(this).html($(this).html().replace('(Blue/White/Green)',""));
});
You've to escape the slashes / and parentheses () using the antislash \ and you should also remove the single quotes ' and .children() also :
$("label").each(function() {
$(this).html($(this).html().replace(/\(Blue\/White\/Green\)/g,""));
});
Hope this helps.
Working snippet :
$("label").each(function() {
$(this).html($(this).html().replace(/\(Blue\/White\/Green\)/g,""));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Text 1 (Blue/White/Green)</label>
You should remove the children(). you need to iterate on the label elements. not on its children
$("label").each(function() {
$(this).html($(this).html().replace(/\(Blue\/White\/Green\)/g,""));
});
this should work
https://jsfiddle.net/L4b2m3a5/1/
Here you go!
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('label').each(function() {
var text = $(this).text();
$(this).text(text.replace('(Blue/White/Green)', 'NOTHING'));
});
});
</script>
</head>
<body>
<label>Text 1 (Blue/White/Green)</label>
</body>
$(":contains('text-to-remove')").contents().filter(function () {
return (this.nodeType == 3 && $.trim(this.nodeValue) == 'text-to-remove');
}).remove();
from another poster
Related
I have two lines of javascript code at a Html body.
<script text="javascript">
Muse.Utils.initWidget('#widgetu94', ['#bp_infinity'], function(elem) { return new WebPro.Widget.Form(elem, {validationEvent:'submit',errorStateSensitivity:'high',fieldWrapperClass:'fld-grp',formSubmittedClass:'frm-sub-st',formErrorClass:'frm-subm-err-st',formDeliveredClass:'frm-subm-ok-st',notEmptyClass:'non-empty-st',focusClass:'focus-st',invalidClass:'fld-err-st',requiredClass:'fld-err-st',ajaxSubmit:true}); });/* #widgetu94 */
Muse.Utils.initWidget('#widgetu386', ['#bp_infinity'], function(elem) { return new WebPro.Widget.Form(elem, {validationEvent:'submit',errorStateSensitivity:'high',fieldWrapperClass:'fld-grp',formSubmittedClass:'frm-sub-st',formErrorClass:'frm-subm-err-st',formDeliveredClass:'frm-subm-ok-st',notEmptyClass:'non-empty-st',focusClass:'focus-st',invalidClass:'fld-err-st',requiredClass:'fld-err-st',ajaxSubmit:true}); });/* #widgetu386 */
</script>
I need to change text ajaxSubmit:true to ajaxSubmit:false only for the second line Muse.Utils.initWidget('#widgetu386'...
I've tried to use this code
<script>
document.body.innerHTML = document.body.innerHTML.replace('ajaxSubmit:true', 'ajaxSubmit:false');
</script>
But after that all of ajaxSubmitare gonna to false.
Maybe I need more Regural Expression?
<script>
document.body.innerHTML = document.body.innerHTML.replace(/MagicRegExp/, 'ajaxSubmit:false');
</script>
I just want find it by '#widgetu386'
Please help me. I don't know what to do.
You use the id of an element to make changes to that specific element. I have never used Muse.Utils.initWidget() personally, but I am assuming that the first paramter of the function is the element id.
<script>
document.getElementById("#widgetu386").innerHTML =
document.getElementById("#widgetu386").innerHTML.replace(
'ajaxSubmit:true',
'ajaxSubmit:false'
);
</script>
I would like a way to detect if a string contains only whitespace of whitespace/formatting html tags without any content.
This is because the specific string is included on a page and should not be shown if only HTML tags ( or whitespace) are included.
Currently I have removed the whitespace using trim. Should I do some something like a replaceAll using rexex to remove html tags?
<div class="well well-large"
ng-if="lc.displayWelcomeMessage.trim() != '' "
ng-bind-html="lc.displayWelcomeMessage"></div>
Are there any ready implementations/libs/functions for this?
Write your javascript like this....
jQuery(document).ready(function(){
jQuery(document.body).on('click', '.click', function(){
var whitDiv = jQuery('.whileSpace').text();
if(whitDiv == '')
{
jQuery('.whileSpace').text('empty Div');
}
else
{
jQuery('.whileSpace').text('have content');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="click">Click</div>
<div class="whileSpace"></div>
You can get the text from the DOM element and then trim it - if the result is an empty string, then you don't have any text in that element. So element.innerText.trim() == '' should do the job.
I need help with some Regexp Javascript/JQuery. I got it working, but I think there is better, simpler solution.
Idea is:
If on button click, target inner html has tags <b></b> anywhere - remove them, else add tag <b> at beginning and </b> at end.
This is the code: jsFiddle example
<input id="button" type="button" value="Bold it"/>
<div id="target">This is <b>sample</b> text</div>
$(function(){
$('#button').click(function(){
var t = $('#target');
//: search for '</b>' too ???
var isFound = t.html().search(new RegExp(/<b>/i));
if(isFound >= 0) //: make 2 replaces into 1 ???
t.html(t.html().replace(/<b>/,'').replace(/<\/b>/,''))
else
t.html('<b>'+t.html()+'</b>')
})
})
Questions are commented in code:
How to search for <b> and </b>
Make .replace(/<b>/,'').replace(/<\/b>/,'') in one .replace(/<b>...?..../,'')
This is not something regex is good for, as there's so many ways in which the expression could fail. See RegEx match open tags except XHTML self-contained tags for humorous reasons as to why.
Try using t.children("b"); to find the instances of <b>...</b/> in your tag.
In this case, try the following:
t.children("b").each(function() {
var contents = $(this).contents();
$(this).replaceWith(contents);
});
Or fully expanded:
$(function(){
$('#button').click(function(){
var t = $('#target');
t.children("b").each(function() {
var contents = $(this).contents();
$(this).replaceWith(contents);
});
});
});
I am trying to find a way to use jQuery to get the first empty div with a certain class. I tried this:
$(".box[html='']").
but it didn't work. Is there an easy way to do this?
That syntax only works for attributes. For HTML contents, you can use .filter():
$('.box').filter(function () {
return $.trim($(this).html()) === '';
}).eq(0);
Edit: The problem with :empty is that line-breaks and spaces are not empty. So:
<div> </div>
<div>
</div>
... won't match.
This should work:
$('.box:empty:first')
http://api.jquery.com/empty-selector/
How about this:
You html:
<div class="empty"></div>
<div class="empty"></div>
your script:
$(document).ready(function(){
$(".empty:empty:first").html("JJJJ");
});
Check this
http://jsfiddle.net/y4Ef2/
var boxEq;
$('div.box').each(function(i,obj){
if (boxEq === void(0)) {
if ($(obj).html() === '') {
boxEq = i;
break;
}
}
});
// this is your div
$('div.box').eq(boxEq);
Try this one:
$('.box').each(function(){
if($(this).text().length == 0)
//box is empty
});
$("div.box:empty").eq(0)
added .eq(0) since you said first empty
Say I have 2 divs:
<div>£ 21.99</div>
<div>£</div>
If I only want to select the div that contains the £ symbol only, how would I go about this?
I have tried:
if ($('div:contains("£")').length > 0)
{
$(this).addClass("pound");
}
I believe the problem is that the browser is transforming the HTML escape characters, so you need to look for the actual character... I tested this and it works
$(document).ready(function(){
$('div').each(function(){
if ($(this).html() == "£"){
$(this).addClass("pound");
}
})
})
$('div:contains("£")').each( function() {
var text = $(this).html();
if (text && text.match(/\s*£\s*/)) {
$(this).addClass('pound');
}
});
Try .filter.
I found another question on SO with pertinent info:
Regular expression field validation in jQuery
tvanfosson's code allows for whitespace either side of the £ sign, but the regular expression could do with a bit of modification:
$('div:contains("£")').each( function() {
var text = $(this).html();
if (text && text.match(/^\s*£\s*$/)) {
$(this).addClass('pound');
}
});
note the '^' and '$' indicating the beginning and end of the string.