Take out property that was placed in the "style" tag

One particular static HTML file that I have consists of the following code:

<style>
  p {
    overflow-wrap: break-word;
    word-break: break-word;
    hyphens: auto;
  }
</style>

<div id="wrapper">
  <p>Insert text here</p>
</div>

<div>
  <input type="checkbox" id="disableOverflowWrap">
  <label for="disableOverflowWrap">Checkbox to disable p { overflow-wrap: break-word; }</label>
</div>

I'm wondering how I can attach an event listener to the checkbox element in order to remove overflow-wrap: break-word; once it is selected?

A snippet of code that I've experimented with is as follows, but unfortunately, it doesn't seem to work:

<script>
const x = document.querySelector('#disableOverflowWrap');
x.addEventListener('click', () => {
  const y = document.querySelector('p');
  y.removeProperty('overflow-wrap');
});
</script>

Answer №1

If you mentioned in the comments that you want the checkbox to toggle the setting on and off, affecting all p elements within the wrapper, one way to achieve this is by toggling a class on #wrapper (or

body</code) that overrides the class on <code>p
. Here's an example:

const cb = document.getElementById("disableOverflowWrap");
const wrapper = document.getElementById("wrapper");
cb.addEventListener('click', ({currentTarget: {checked}}) => {
    wrapper.classList.toggle("override-overflow-wrap", checked);
});
#wrapper.override-overflow-wrap p {
    overflow-wrap: normal;
}

To better visualize the change, I've also added color: green to the original rule and color: black to the override. Here is a live example:

const cb = document.getElementById("disableOverflowWrap");
const wrapper = document.getElementById("wrapper");
cb.addEventListener('click', ({currentTarget: {checked}}) => {
    wrapper.classList.toggle("override-overflow-wrap", checked);
});
p {
    overflow-wrap: break-word;
    word-break: break-word;
    hyphens: auto;
    color: green;
}
#wrapper.override-overflow-wrap p {
    overflow-wrap: normal;
    color: black;
}
<div id="wrapper">
    <p>First paragraph</p>
    <p>Second paragraph</p>
    <p>Third paragraph</p>
</div>

<div>
    <input type="checkbox" id="disableOverflowWrap">
    <label for="disableOverflowWrap">Disable p { overflow-wrap: break-word; }</label>
</div>

Answer №2

To implement word breaking, simply insert the new class .break-word with the property word-break: break-word and then use JavaScript to remove the existing class.

<style>
  p {
    overflow-wrap: break-word;
    hyphens: auto;
  }
  .break-word {
    background: red;
    word-break: break-word;
  }
  a:any-link {
    hyphens: none;
  }
</style>

<div id="wrapper">
  <p class="break-word">Here goes some text</p>
</div>

<div>
  <input type="checkbox" id="disableOverflowWrap">
  <label for="disableOverflowWrap">Disable p { overflow-wrap: break-word; }</label>
</div>

<script>
const x = document.querySelector('#disableOverflowWrap');
x.addEventListener('click', () => {
  const y = document.querySelector('p');
  y.classList.remove('break-word');
});
</script>

Answer №3

The CSS property overflowWrap can be accessed through the style object. To set it to its default value of normal, you can use the following script:

<script>
const button = document.querySelector('#disableOverflowWrap');
button.addEventListener('click', () => {
  const paragraphs = document.getElementsByTagName('p');
      
      for(let index = 0; index < paragraphs.length; index++) {
          paragraphs[index].style.overflowWrap = "normal";
      }
});
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jQuery's load method is not responsive when prompted via load

I recently came across a unique voting system on that caught my eye. The code they used is as follows: $(".vote").click(function() { $(this).load($(this).attr('href')); console.log("voted"); return false; }); accompanied by this H ...

Troubleshooting: AngularJS ng-options failing to populate the list of objects

Having trouble populating a combo box in AngularJS. Here is the controller code snippet: var app = angular.module('app'); app.controller('roadmap', function($rootScope, $scope, $http, $q, leFactory) { $rootScope.activityInfoList=[ ...

"Adding an active class to a link based on the current page and locale: A step-by-step

Looking to add an active class to the subheader menu on the active page, but facing issues when changing the locale in the link. While it works for links like my-site/my-page, it doesn't work for links like my-site/fr/my-page. Utilizing Storyblok head ...

Incorporating multiple colors into a div with jQuery: A guide

I am looking for a way to create a legend by merging a specified number of colors within a div. I came across this useful Sample code that I have referenced. $.each(Array(50), function() { $("<div>").appendTo(document.body); }); var divs = $(&a ...

Enhance your React Native app: Utilizing dynamic string variables with react-native-google-places-autocomplete query

I have encountered an issue while attempting to pass a dynamic country code to restrict search results. Here is the code in question: let loc = 'de' <GooglePlacesAutocomplete placeholder="Search" autoFocus={true} onPress ...

Understanding the Importance and Benefits of Using the Classnames Utility in React Components

Can you break down for me the purpose of utilizing the Classnames utility in React code? I've reviewed the Classnames documentation, but I'm still struggling to comprehend why it is used in code like this: import classnames from 'classnames ...

Ways to extract the initial layer of information from a JSON document

I have a JSON file named movie.json stored externally, and it follows this format: { "action": [ { "id": "1001", "name": "Matrix" }, { "id": "1002", & ...

Troubleshooting: Why is the AngularUI Modal dialog malfunctioning

I'm currently working on integrating an angularUI modular dialog into my application. Here is a snippet from my controller.js file: define([ 'app' ], function(app) { app.controller('TeacherClasses', [ '$scope', &apo ...

Unable to retrieve observable modifications

In my code file for handling reports, named report.service.ts, I have a method that retrieves reports data. This method simply returns a constant array of report objects from a mock source. Here is the implementation: @Injectable() export class ReportServ ...

Ways to incorporate design elements for transitioning focus between different elements

When focusing on an element, I am using spatialNavigation with the following code: in index.html <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfb5acf2acafbeabb6beb3f2b1bea9b6 ...

Issue with setting a cookie on a separate domain using Express and React

My backend is hosted on a server, such as backend.vercel.app, and my frontend is on another server, like frontend.vercel.app. When a user makes a request to the /login route, I set the cookie using the following code: const setCookie = (req, res, token) = ...

Updating an HTML input field property with JavaScript is not reflecting the changes

I am currently working on a form with two fields: stationery type and stationery request quantity. The stationery request quantity field only accepts numerical input. The minimum quantity that can be entered in this field depends on the value selected in t ...

Error encountered while retrieving data from Firebase and storing it in an array within an IONIC application

I am currently working on a function that retrieves data from Firebase's real-time database and stores it in an array for mapping in React. However, I am encountering a TypeScript error that I'm having trouble resolving. The error message reads ...

Is it possible to update a MobX state when a message is received from Firebase in the background?

I've set up Firebase in my project like this: import { initializeApp } from 'firebase/app'; import { getMessaging, getToken, onMessage, isSupported } from 'firebase/messaging'; import store from './flag'; ...

Make sure that the iframe loads the next page with enough force to break out

My dilemma involves an iframe that loads the new tab page. When a user clicks on the thumbnail, it opens within the iframe. My goal is to have any subsequent load in the iframe redirected to window.top. Is there a way to achieve this without manually setti ...

Guide to automatically update div with new table rows with the help of Ajax

Can you assist me in updating the div called "table" that contains a table fetching rows from the database? <div id="table"> <h1 id="Requests"> <table></table> </h1> </div> <button id="refresh-btn"&g ...

What is the best way to pass a value back to the main function from an async.eachOfSeries function?

Currently, I am utilizing the async npm library in my project. I am interested in finding a way to return the value of 'someVar' back to the main function. The documentation indicates that it returns a promise if a callback is not provided. Howe ...

The page is not responding after closing the modal dialog

My web application is built using Spring Boot for the backend and Thymeleaf for the front end. The app displays all available groups for users to review. When a user clicks on a group, the documents within that group are listed in a tabular form. Clicking ...

Steps to obtain an Element binding from a Svelte component

Is it possible to retrieve the bounding client rect of an anchor element? I typically mark this using the syntax bind:this={someVariable}. However, when I add this code to a Svelte component, I receive a different object that Svelte uses to represent a c ...

Animating toasts in Bootstrap

Exploring the options available at https://getbootstrap.com/docs/4.3/components/toasts/ To customize your toasts, you can pass options via data attributes or JavaScript. Simply append the option name to data- when using data attributes. If you're lo ...