Add class or id dynamically when clicked

How can I dynamically change the CSS color of a roller-banner div based on user input?

I have a codepen setup where I want to capture the class of the clicked element (e.g. colour3), and then apply that as an ID or class to the roller-banner div.

Codepen Link: https://codepen.io/scottYg55/pen/WNNgxWZ

$(".changecolours > div").on('click', function() {
  var colourChange = this.value;
  $('.roller-banner').attr('class', colourChange);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="changecolours">
  <div class="colour1">A</div>
  <div class="colour2">B</div>
  <div class="colour3">C</div>
  <div class="colour4">D</div>
</div>

<div class="roller-banner">
  <img src="http://www.project-progress.co.uk/dropbox/global/wp-content/uploads/2019/11/roller-banner-nodetails.png" class="static">

  <div class="halft-colour"></div>
  <div class="halfb-colour"></div>

  <div class="dlogo">
    <img src="http://www.project-progress.co.uk/dropbox/global/wp-content/uploads/2019/11/select-dropbox.png" />
  </div>

  <div class="instertxt">
    <div id="yourtextlive"></div>
  </div>

  <div class="dpimg">
    <img src="http://www.project-progress.co.uk/dropbox/global/wp-content/uploads/2019/11/banner-insert-img.png" />
  </div>

  <div class="dplink">
    dropbox.com
  </div>

</div>
</div>

Answer №1

The main issue here is that you are trying to manipulate the value property of a div, which is not valid. It would be more appropriate to store the "colour" as a data attribute instead.

Another suggestion is to store this selection within the roller-banner element, making it simpler to switch classes when a different one is chosen.

$(".changecolours > div").on('click', function() {
  var colourChange = $(this).data("color");
  $('.roller-banner').removeClass(function(){ return $(this).data("selected-color")})
                     .data("selected-color",colourChange)
                     .addClass(colourChange);
});
.colour1{ background-color:red}
.colour2{ background-color:green}
.colour3{ background-color:orange}
.colour4{ background-color:blue}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="changecolours">
  <div data-color="colour1">A</div>
  <div data-color="colour2">B</div>
  <div data-color="colour3">C</div>
  <div data-color="colour4">D</div>
</div>

<div class="roller-banner">
  <img src="http://www.project-progress.co.uk/dropbox/global/wp-content/uploads/2019/11/roller-banner-nodetails.png" class="static">

  <div class="halft-colour"></div>
  <div class="halfb-colour"></div>

  <div class="dlogo">
    <img src="http://www.project-progress.co.uk/dropbox/global/wp-content/uploads/2019/11/select-dropbox.png" />
  </div>

  <div class="instertxt">
    <div id="yourtextlive"></div>
  </div>

  <div class="dpimg">
    <img src="http://www.project-progress.co.uk/dropbox/global/wp-content/uploads/2019/11/banner-insert-img.png" />
  </div>

  <div class="dplink">
    dropbox.com
  </div>

</div>
</div>

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

In Internet Explorer, the toggle div expands to fill the available space, but this behavior is not consistent in

Utilizing the toggle function in jQuery, I created a mechanism to switch between 4 different blocks of content. You can view a demonstration of how the functionality operates correctly in Google Chrome here: If you encounter any issues with the functiona ...

Expand a div without causing any displacement to surrounding elements

If you have 4 colored divs (red, yellow, green, blue), with 2 per row, and need to expand the second (yellow) without creating a gap under the red, check out this solution. The blue will be positioned underneath the expanded yellow. For a live example, vi ...

Is it possible to substitute the input field in the filter component with a Material UI text field instead?

I am attempting to replace the input field for filter components with a text field from materialUI. Despite successfully displaying the input field by creating a new component, it is not filtering contents as desired. My goal is simply to replace the inpu ...

How can HTML text be highlighted even when it's behind a transparent div?

In my React application, I am facing an issue with a transparent div that serves as a dropzone for draggable elements. This div covers text and also contains children elements that may have their own text content. <Dropzone> <SomeChild> . ...

Aligning form fields in a HTML form using Bootstrap

In my extensive form, several fields within the setup encounter the same issue. Specifically, a checkbox below 'contact telephone number' causes the surrounding content to become misaligned. https://i.sstatic.net/8qI8Z.png My attempts to resolv ...

Is there a method to adjust the styling of my <header> once the page hits a specific #anchor point?

Seeking some assistance with a website design challenge I am facing. I am currently working on a one-page portfolio site with four #anchor points that serve as "pages". Each div has a height of 100% to fill the entire browser height. The header, which co ...

establishing the dimensions of table data cells

I'm facing a challenge with setting the width and height for table data that changes dynamically based on different values. The dimensions of the table itself are not definite, so I need to find a solution. Here's the code snippet I'm curren ...

Using CSS to overlay a background image on top of a background gradient

Struggling to get both a background gradient and a background image (transparent PNG) to display in my div. No matter what, only the color gradient shows up while the image remains hidden. If I use a solid color instead of a gradient, the image displays ju ...

Conceal a script-generated div using CSS styling

Using the code below, you can create an HTML chatbox with a link in the top panel and multiple child divs. The structure is as follows: cgroup is the parent of CBG, which is the parent of CGW, and CGW is the parent of the div I want to hide. How can I u ...

Troubles with z-index: issues arise when combining relative and absolute positioned divs

Struggling with my z-index arrangement here. In a nutshell, I have an absolute div positioned beneath a relative div called container. Inside the container, there are three divs: left, center, and right, all with absolute positioning. I am trying to get ...

The controller element in AngularJS becomes undefined when invoked within a directive

Presented below is a snippet of my controller: myApp.controller('WizardController', function ($scope, $http) { $scope.user = { addressline1: null, cobuyer: null, validate: null, cobuyerfirstname: null, cobuyerlastname: null, ...

I need help figuring out how to retrieve the full path of an uploaded file in JavaScript. Currently, it only returns "c:fakepath" but I need

Is there a way to obtain the complete file path of an uploaded file in javascript? Currently, it only shows c:\fakepath. The file path is being directly sent to the controller through jquery, and I need the full path for my servlet. Are there any alte ...

Mastering the Art of Modifying HTML with Node.js

Is it possible to manipulate HTML using Node.js? 1. First, I need to retrieve data from a database. 2. Then, I need to modify or add HTML code within Node. In essence, the goal is to fetch data and integrate it into an HTML file. The JavaScript snippet ...

What is the best way to resize an image to fit its surroundings within a nested box?

Have you ever heard of a website called SPAM? It has a unique homepage that I want to replicate using JavaScript, jQuery, and some CSS. My main challenge is figuring out how to adjust the image size to match the center box on the page. I want to create th ...

Discovering the reason behind a DOM element's visual alteration upon hovering: Where to start?

Visit this page, then click on the next button to navigate to the time slots section. As you hover over any time slot, you will notice a change in appearance. Despite inspecting Chrome's developer tools, I was unable to locate a style with a hover dec ...

Disable the mouseenter event in jQuery when the window is resized

I am facing a challenge with my code. I have two different functions that are triggered based on the screen size - one for desktop and one for mobile. The issue arises when transitioning from a desktop to a mobile view, as the hover/mouseenter effect still ...

Troubleshooting a problem with jQuery and Drupal behavior when the document is ready

Encountering a strange issue with my jQuery code within Drupal 7. I implemented the following snippet : (function ($) { Drupal.behaviors.exampleModule = { attach: myPopUpFunction..... })(jQuery); Interestingly, on Mac browsers, the popup loads af ...

What is the best method to achieve a width of 100% for an element (textarea) that has unspecified borders and padding, while also including the borders and padding

Native borders for input controls in various browsers often look more visually appealing compared to those set with CSS. However, the thickness of these native borders and padding can vary across different browsers, making it difficult to predict beforehan ...

Having difficulty with navigating from the jquery css click event within AngularJS

I successfully implemented a click binding using AngularJS with the following code: data-ng-click="vm.newCard()" The newCard() function is defined as: $location.path("xxxx/card/0"); This setup is navigating correctly. However, when using jQuery for a ...

Efficiently Loading AJAX URLs using jQuery in Firefox

setInterval(function(){ if(current_url == ''){ window.location.hash = '#!/home'; current_url = window.location.hash.href; } else if(current_url !== window.location){ change_page(window.location.hash.split('#!/&apo ...