What is the process to switch the div's contenteditable attribute value from "false" to "true"?

I am currently experimenting with angular2 and I have a need to alter the value of a div- I want to change all contenteditable="false" to contenteditable="true" on my HTML page.

Note that at times the contenteditable="false" attribute may be added to a DIV tag, P tag, h1 tag, or some other element.

This is how the html appears:

<button (click)="selectDiv()">select div</button>
<section>
      <div class="content">
        <h1 contenteditable="false" >Resize your browser and see how they adapt.</h1>
        <h1 contenteditable="false" >text2.</h1>
      </div>
      <div class="content" contenteditable="false">
        <p>Content 2.</p>
        <h1>text2.</h1>
      </div>
</section>

I need guidance on what code I should include in explore.ts so that when the button is clicked and selectDiv() is called, the function will execute and convert all instances of contenteditable="false" to "true"

Please keep in mind that there could be any number of contenteditable="false" occurrences in my code.

selectDiv(){
//what should i write here
}

Answer №1

Here's a way to tackle this in Angular:

Start with the HTML

<button (click)="selectDiv()">select div</button>
<section>
  <div class="content">
    <h1 [contentEditable]="contentEditable">Resize your browser and see how they adapt.</h1>
    <h1 [contentEditable]="contentEditable">text2.</h1>
  </div>

  <div class="content" [contentEditable]="contentEditable">
    <p>Content 2.</p>
    <h1>text2.</h1>
  </div>
</section>

Next, add the TypeScript Controller

...
private contentEditable: boolean = false;
...
selectDiv(): void {
    this.contentEditable = true;
}
...

Answer №2

To update the value of an attribute, you can query for the attribute and use the attr property.

This code selects all h1 elements that have the contenteditable attribute.

function selectDiv() {
  $("h1[contenteditable='false']").attr('contenteditable', true)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="selectDiv()">
  select div</button>
<section>

  <div class="content">
    <h1 contenteditable="false">Resize your browser and see how they adapt.</h1>
    <h1 contenteditable="false">text2.</h1>
  </div>

  <div class="content" contenteditable="false">
    <p>Content 2.</p>
    <h1>text2.</h1>
  </div>

</section>

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

Retrieve the user ID by utilizing the phonegap login feature integrated with Facebook

I'm working on a feature that lets users upload photos to my server once they log in using their Facebook credentials. I'm currently using the phonegap facebook plugin for Android. How can I retrieve their unique user ID from the Facebook SDK? Al ...

What is the best way to update the values of an array of object properties using an input form in JavaScript?

I'm currently learning node.js, express.js, and handlebars I am working on creating two forms for a blog/news page: an add form and an edit form. /addproject: app.get('/addproject', function(req, res) { res.render('addprojec ...

Rejuvenating your HTML content with AJAX over time

My HTML page contains links to charts that refresh every time the page is reloaded. A friend mentioned that AJAX can automatically refresh the chart at specified intervals without reloading the entire HTML page. I would appreciate any help with the HTML ...

Initiate Action Upon Visibility

After discovering this script that creates a counting effect for numbers, I realized it starts animating as soon as the page loads. While I appreciate its functionality, I would prefer it to only initiate when the element is in view; otherwise, the animati ...

Enhancing a Given Interface with TypeScript Generics

I am looking to implement generics in an Angular service so that users can input an array of any interface/class/type they desire, with the stipulation that the type must extend an interface provided by the service. It may sound complex, but here's a ...

Submit an Ajax form to process data in PHP upon selecting a radio button

I am troubleshooting an issue with a form that is not submitting data to processor.php for processing and storing responses in a database. Ensuring that the form submission does not cause a page refresh is crucial, as there is an iframe below the form tha ...

When combining Angular4, Protractor, Cucumber, TypeScript, and webpack-dev-server, Angular seems to have gone missing

Operating System: macOS Sierra 10.12.4 Node Version: v7.9.0 Npm Version: 5.0.3 Cucumber-js Version: 2.3.0 Protractor Version: 4.0.14 TypeScript Version: 2.2.2 Webpack-dev-server Version: 2.4.5 I encountered an issue while running end-to-end tests. When tr ...

Identify Bootstrap modal to modify destination of keypress input

I am working on a piece of code that detects keypress events and focuses input towards a searchbar with the id "search". $( document ).keypress(function() { $("#search").focus(); In addition, I have a bootstrap modal dialog containing input fields ...

The transparent background causes the vertical menu to malfunction on IE8

This is the first website I am working on: www.olgoya.com The issue I am facing is that all major browsers, except IE8, display the submenu even when the mouse hovers over the 'portfolio' item. Upon investigation, I found that the problem lies w ...

What is the method for displaying script commands within package.json files?

With a multitude of repositories, each one unique in its setup, I find myself constantly referencing the package.json file to double-check the scripts. "scripts": { "start": "npm run dev" "build:dev": "N ...

Encountering an issue with Angular 18 and SignalR while compiling the application: [ERROR] Unable to resolve "url", "https", "http", "util" from eventsource

Looking to implement real-time technology in an Angular 18 application? SignalR is the chosen technology, but encountering issues during the package setup and build process. The error might be linked to the build process, potentially due to Angular moving ...

Display a loading animation while waiting for the AJAX response

I am working with a seat chart in SVG format on my website. I want to display a loading gif when a user clicks on a seat, and then hide the loading gif once the AJAX response is returned. However, the code I have written for this functionality is not wor ...

Show a picture upon hovering the button

Welcome to my website. My goal: Show an image when a user hovers over the links. I must be making some silly error, but I can't pinpoint it. This is what I've attempted so far: HTML <ul class="nm"> <li><a href="#">Cork& ...

Activating the information from the second dropdown menu once it has been chosen in the first dropdown menu using jQuery

Is there a way to create a jQuery function that will only allow the value from the second dropdown to be enabled if a value is selected in the first dropdown? For example, if I select Shiva in the first dropdown, it should then be disabled in the second d ...

Eliminate lower values in select 1 if the value selected in select 2 is higher

I am struggling with a particular piece of code and could really use some assistance. Within my project, I have two select boxes. The first box allows users to select the "from" year, while the second box is for selecting the "to" year. What I'm tryi ...

Setting up TypeScript in Jest without the need for webpack

Currently, I'm developing an NPM module using TypeScript without the use of Webpack for compiling scripts. I need some guidance on configuring Jest to properly run tests with TypeScript files. Any recommendations? // test.spec.ts import {calc} from ...

Select characteristics with designated attribute types

Is there a way to create a type that selects only properties from an object whose values match a specific type? For example: type PickOfValue<T, V extends T[keyof T]> = { [P in keyof (key-picking magic?)]: T[P]; }; I am looking for a solution w ...

InnerHTML syntax for creating the button in the cell is not functioning properly with the HTML onclick event

I'm facing an issue with my code where I am trying to insert a button into a table cell. The button has an action assigned to it using the onclick attribute. However, no matter how I try to use single quotes with or without backslashes in the syntax o ...

Changing Coordinated Universal Time to Pacific Standard Time with Moment Timezone in JavaScript

I am attempting to set the variable "formattedLocalTime" to Pacific time using the following snippet of code. Despite the fact that the chrome debugger shows "locTime" as "Tue Sep 30 2014 16:17:25," which is the expected value, the formattedLocalTime on t ...

Using ionic-v4 to browse through a application via its URL pathways

I am facing an issue with navigating to specific pages on my website using the URL. For example, I want to access page 1 by typing in the address bar: localhost:8100/mySite/page1 and go directly to page 1 localhost:8100/mySite/page3 and navigate to pag ...