Forum Moderators: open

Message Too Old, No Replies

Userscript help - simple replacement of attribute

         

Adam_C

12:20 pm on Nov 29, 2019 (gmt 0)

10+ Year Member



I want to build a userscript to change a style attribute within a <section> tag, along these lines:


<section class="adam" style="thing: annoying" >


where I want to switch it to something like

<section class="adam" style="anotherthing: ok" >


Any pointers appreciated

NickMNS

3:35 pm on Nov 29, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This should do what you would like. Note that I used actual style values for the demo. The background of the "section" should appear as color "peachpuff". But the js removes that style and replaces it with color=red, which changes the font color to red and that is what is displayed. There is not event listener to trigger the code. This happens immediately at page load so the peachpuff background is never displayed.

HTML:
<section class="adam" style="background: peachpuff" >Section</section>


JS:

<script>
let elem = document.querySelector('section');
elem.style.background = "";
elem.style.color = "red";
</script>


demo: [jsfiddle.net...]

Adam_C

4:54 pm on Nov 29, 2019 (gmt 0)

10+ Year Member



Thanks Nick