There's two potential issues here. Firstly, you need to create a jQuery object from string
and save a reference to it. If you don't do that you'll lose the changes you made to the HTML.
Secondly you need to use filter()
instead of find()
, as there is no single root level
node to search from in the HTML string. Try this:
let string = '<h1 style="color: red;">H1 Foo</h1><h2 style="color: yellow;">H2 Foo<p style="color: green;">P Foo</p><p style="color: blue;">P Foo</p></h2>';
let $string = $(string);
$string.filter('h1').removeAttr('style');
$('body').append($string);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
What you can do is, create a virtual element in the code and place the content in it. then find the element and remove the attribute:
let str = '<h1 style="lots of class">h1</h1><h2> <p style="bunch of class"> h2 > p</p> <p style="bunch of class"> h2 > p </p></h2>';
let $div = $('<div>')
$div.html(str).find('h1').removeAttr('style');
$(document.body).append($div.html())
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
We can achieve this through regex.
const string = '<h1 style="lots of class"> </h1><h2> <p style="bunch of class"> </p> <p style="bunch of class"> </p></h2>';
const stringNoStyle = string.replace(/style="(.*?)" /g, '')
// <h1> </h1>
// <h2>
// <p> </p>
// <p> </p>
// </h2>
i need to check whether the date is between minDate and maxDate. but when i try to compare with minDate, i should get valid as false but am getting true.. let minDate = "27-05-2019"; ...
i need to check whether the date is between minDate and maxDate. but when i try to compare with minDate, i should get valid as false but am getting true.. let minDate = "27-05-2019"; ...
The following snippet is from "Example 5" from this thread on JS and closure. Alternatively, this other thread sort of gets at what I'm curious about. function buildList(list) { var result = []; ...
The following snippet is from "Example 5" from this thread on JS and closure. Alternatively, this other thread sort of gets at what I'm curious about. function buildList(list) { var result = []; ...
I want to make my component more reusable. In the component I'm binding two values with ngModel: elem.key and elem.value. The problem is that wherever I want to use this component, the element has to ...
I want to make my component more reusable. In the component I'm binding two values with ngModel: elem.key and elem.value. The problem is that wherever I want to use this component, the element has to ...
So im trying to get a div with an iframe in it to reload every 20 sec, i have searched a bit on it, got a code to work on local, but when i upload it to the internet, it wont work, WHY ?. Are there ...
So im trying to get a div with an iframe in it to reload every 20 sec, i have searched a bit on it, got a code to work on local, but when i upload it to the internet, it wont work, WHY ?. Are there ...