How can I use JavaScript to loop through a specific attribute?

I have implemented ckeditor inline on a website I created. While it allows me to save data to a database, the issue arises when ckeditor automatically applies its own classes and attributes to certain elements where contenteditable is enabled. It also removes some existing classes, disrupting the styling of my webpage. I am looking for a way to remove these ckeditor-set classes and attributes before displaying the content in the browser. Is there a JavaScript solution that can loop through elements with content editable set to true and achieve this?

Answer №1

Simple JavaScript Method:
var elements = document.getElementsByTagName("*");
for(var index = 0, length = elements.length; index < length; index++)
{
if (elements[index].contentEditable)
{
// Your logic here
}
}

JQuery Approach:

According to dandavis:

$("*[contentEditable]").each(function()
{
    // 
});

An important note:

Since .contentEditable doesn't always return a boolean value, you need to verify if the element actually has the contentEditable attribute like so:

var elements = document.getElementsByTagName("*");

for(var index = 0, length = elements.length; index < length; index++)
{
    if (elements[index].contentEditable === "true" || elements[index].contentEditable === "")
    {
        // Your logic here
    }
}

To remove the contentEditable attribute:
Using jQuery, you can achieve this:

$("*[contentEditable]").each(function()
{
    this.prop("contentEditable", false);
});

Plain JavaScript Example:

elements[index].contentEditable = "false";

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

Error #301 in React: Infinite loop causing repeated rendering

import React,{createContext,useState} from 'react'; export const UserContext = createContext(); export const UserProvider = props =>{ const [ user,setUser ] = useState({ logged: false, User_info: { } }) if( ...

Retrieving data from a promise in a Node API when making a fetch call in React

Currently, I am developing a React application that interacts with a Node/Express REST API located on my local machine. The API responds with a Sequelize object via a simple res.json call, which I am accessing through a custom service. While I plan to stor ...

Require assistance with understanding the aes-256-cbc encryption using oaepHash

Procedure for Secure Data Encryption: Generate a random 256-bit encryption key (K_s). For each Personally Identifiable Information (PII) value in the payload: 1. Pad the plaintext with PKCS#7 padding. 2. Create a random 128-bit Initialization Vector (IV). ...

Creating a stunning image carousel in Vue by integrating a photo API: step-by-step guide

Trying to figure out how to create an image carousel in Vue using photos from an API. Currently able to display the photos using: <section class="images"> <img v-for="image in images" :key="image.id":src="image.assets.large.url"> &l ...

Is there a way to ensure that the bootstrap popover always appears in the center of the screen, similar to a Modal

Hi, I need to implement popovers or tooltips from Bootstrap. I am currently calling the popovers using a classname on many buttons. Is there a way to have all button popovers open in the center of the screen using JavaScript? Thank you. ...

Is there a way to verify if all the values in an array of objects are identical?

In this scenario, my array consists of entries with identical address IDs but different phone types and numbers. I am in need of assistance with iterating through the array to extract the phone type and number when the address ID matches. I seem to encount ...

Create custom styles for Android applications using CSS or themes programmatically

As I work on developing an application, I am interested in implementing a unique approach... To retrieve a CSS file from the server and apply it to the activity layout. To create a string file for styling or theming and integrating it into the layout. I ...

Tips for resolving flickering animations in CSS and JavaScript

Is it possible to dynamically set a scale and margin for an element in order to center it fluidly using the wheel event? I am aiming to achieve a smooth transition while also adjusting scroll position on the wrapping element in a fluid manner. In the prov ...

Uploading multiple textures for .obj files in ThreeJS

I've been attempting to upload a .obj file along with multiple .png and .jpg files that serve as its textures. However, I'm struggling to figure out how to properly handle these textures upon upload. Here is the code I have so far: var loader = ...

What could be the reason for the malfunction of jQuery's show() function?

Using jQuery, I have implemented a functionality to hide a div using the hide() method. However, upon clicking a link, the div is supposed to show but unexpectedly disappears after appearing briefly. HTML Code Snippet <div id="introContent"> & ...

Applying a gradient overlay to an image using CSS

Feeling a bit frustrated with this issue. I'm working on my Joomla website utilizing Phoca Gallery, and the code it generates includes the following: <img class="pg-image" src="/images/phocagallery/other/thumbs/phoca_thumb_m_something.jpg" alt="So ...

How to toggle hidden links with AngularJS or vanilla JavaScript with a click事件

When the "Show all" button is clicked, I would like to display all elements. If the "1st half" link is clicked, I want to show "abc", "def", and "ghi". When the 2nd link "2nd half" is clicked, I want to display "jkl", "mno", and "pqr". Then, when the "show ...

Steps to have the initial link redirect to a designated webpage

I am working with tables that have links defined in the HTML. I want to modify the behavior so that the first link opens a different URL, for example: john.com. Is it possible to achieve this? Specifically, I want the link associated with the first name ( ...

The Bootstrap tooltip fails to display the visual style of the tooltip, only showing the data

Following the Bootstrap documentation for tooltips, I have tried to make the tooltip function work properly. However, when I hover over the text, the tooltip text Some tooltip text! only shows up with the alt="" function, but not styled as described in the ...

Action type is not recognized: modification

After browsing through multiple forums, I haven't found a solution that works for my particular issue with my VueJS app. The problem arises when I try to input something in my first component. Below is the code snippet: main.js import Vue from &apos ...

determining the dimensions of a pixel based on the pixel density measurement

Can someone help me double-check my calculations? I am trying to figure out how many pixels are in the span of 13.6 millimeters for a specific device that has a resolution of 224.17 pixels per inch. (Given that 1 inch = 224.17ppi, therefore: 1 centimeter ...

Develop a carousel component using Vue.js

I'm currently working on a dashboard that needs to display cards, but I'm running into an issue. I want only four cards visible at a time, and when you click the arrow, it should move just one step to the next card. For example, if cards 1-4 are ...

Issue encountered with edges helper and a partly opaque object rendering

My goal is to create a realistic earth using three.js, similar to this example, which is an improvement from this one. However, I am facing an issue where the rendering order of the sky, earth, and atmosphere is not being properly interpreted by the render ...

Passing event handlers to Client Components within NextJS 13 and using the <button onClick={}> element is not supported

Oops! It looks like you can't pass event handlers to Client Component props. If you want your component to be interactive, consider converting some of it to a Client Component. const reqHelp = () => { Swal.fire({ title: '1', ...

Encountering an error when attempting to include React TypeScript onChange with a Material UI switch component

I'm working on implementing a show/hide functionality using a switch. I want the component to be displayed when the switch is turned on and hidden when it's turned off. Here's the code I've written: const VirtualEventSection = ({ con ...