RSS Daily tech news
  • Ordinary WiFi can now identify people with near perfect accuracy
    Scientists in Germany have demonstrated a startling new form of surveillance: identifying people using nothing more than ordinary WiFi signals. By analyzing how radio waves bounce around a room, researchers can effectively “see” and recognize individuals — even if they are not carrying a device and even if their phone is turned off.
  • New quantum sensor could count individual photons and hunt dark matter
    Researchers have built an ultra-sensitive sensor capable of detecting unimaginably small amounts of energy — below one zeptojoule. The breakthrough relies on fragile superconducting materials that react to even the slightest temperature change. This level of precision could improve quantum computers, enable photon counting, and even help scientists detect elusive dark matter particles from space.
  • New quantum algorithm solves “impossible” materials problem in seconds
    A new quantum-inspired algorithm has cracked a problem so massive that conventional supercomputers struggle to even approach it. Researchers used the method to simulate extraordinarily complex quantum materials known as quasicrystals, opening the door to powerful new quantum devices and ultra-efficient electronics. The work could help scientists design advanced topological qubits and materials for future […]
  • The hidden atomic gap that could break next-generation computer chips
    A major obstacle may be standing in the way of the next generation of ultra-tiny computer chips. Researchers discovered that many promising 2D materials lose their advantages because an invisible atomic-scale gap forms when they are combined with insulating layers. That tiny gap weakens electronic performance and could prevent further miniaturization. The team says new […]
  • Stanford’s new chip boosts light 100x with surprisingly low energy
    Researchers at Stanford have developed a compact optical amplifier that dramatically boosts light signals using very little power. By recycling energy inside a looping resonator, the device achieves strong amplification with minimal noise and wide bandwidth. Its efficiency and small size mean it could run on batteries and be integrated into consumer electronics. This breakthrough […]
  • Scientists capture electrons forming strange patchy patterns inside quantum materials
    Researchers have, for the first time, directly visualized how electronic patterns known as charge density waves evolve across a phase transition. Using cutting-edge microscopy, they found these patterns form unevenly, breaking into patches influenced by tiny structural distortions. Unexpectedly, small pockets of order persist even above the transition temperature. This reveals that electronic order fades […]

Change color with just the click of a button

by Florius
Featured image of changing colors with just a button click

Hi all. In this blog post I will explain how an element’s background can change with a click of a button.

To do this, you just need elementor (or have a basic understand on HTML), without the need for the elementor pro version. Below I give you a very basic example of what it is I will talk about in this post. I want to change the background color of another widget with the use of a button. Give the two buttons a try and see the result. Obviously, this isn’t limited to just coloring the background, but almost anything can be changed with the click of a button.

Change the background of this text widget.

1. Why do we want this?

It is quite simple, interaction. Websites want visitors to be engaged with the content, preferably through interaction. At the same time, I, as the content creator, do it to make my content more appealing for everyone, including myself. I am not the most creative person, and you will not find flashy widgets on my website. When you click a button, you expect something to happen, may it be a link to another page, or even on the same website.

I started with this topic, when writing my post on resistor color codes and thought it might be interesting to create an interactive post, as similar figures have already been made a thousand times. The idea was to have a background image of a resistor, with rectangular elements that represent the different color bands. The buttons can be used change color in those bands, making it look like an actual resistor. At the same time, I added some logic that automatically calculates the resistance value; hence it is not only for the looks.

2. What do we need?

There are three parts to this story, namely HTML, CSS, and Javascript. They are the three core technologies in web development. For those that use the Elementor plugin, widgets can be used to create anything from buttons, images, carrousel wheels, you name it. These widgets will replace the HTML and CSS coding. However, I will still briefly explain them:

  • HTML (hypertext markup language) is used to structure your website. It defines the layout and provides elements, such as heading, paragraphs, links, etc.
  • CSS (cascading style sheet) is used to style your website, giving it fonts, colors and even animations. CSS makes your website more appealing.
  • Javascript is what gives functionality to your website, and makes it interactive. You can handle events, such as button clicks and more.

In wordpress & elementor, there is a single widget for all three coding langauges. This is the HTML widget (), and it should be placed somewhere on the same website as where your buttons are located. The actual widget won’t be visible. The figure below shows how this widget looks like.

HTML widget in WordPress elementor

As mentioned, it is possible to use HTML, CSS and Javascript in this widget, given that you use the correct syntax. For CSS you will place your code between, while for Javascript the code should be between to function. HTML code can be added without anything.

Fortunately for us, with the button widget of Elementor, we do not need to work with HTML and CSS anymore (although it is still very possible to do so in Elementor Pro). Similarly, you might want to consider what your target is, in my example above it was a text editor widget, but you could also use a container, a spacer, etc.

Each element that requires functionality should have its own “CSS Classes” name; which basically labels that element with its unique identifier (in fact CSS ID is the unique identifier, but you can assign one or more classes to any element). This option can be found in the advanced tab of the widget, and is shown in the two figures below.

As you can see, we named the button “button” and the text editor “target-element”.

Button > Advanced > CSS Classes
Button > Advanced > CSS Classes
Target > Advanced > CSS Classes
Target > Advanced > CSS Classes

3. Source file - Change color

In the HTML widget, we write the code. The following code is just to give the rest its functionality, hence we only encorporate javascript in here.

				
					<script type="litespeed/javascript" data-src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type="litespeed/javascript">jQuery(document).ready(function($){$('.button').on('click',function(event){event.preventDefault();$('.target-element').css({'background-color':'#093061','color':'#FAFAFA'})})})</script> 
				
			
  1. Including jQuery Library:

    • : This line includes the jQuery library from a CDN (Content Delivery Network). It allows you to use jQuery in your web page.
  2. Document Ready Function:

    • jQuery(document).ready(function($) { ... });: This code wraps everything inside it to ensure that it runs only after the DOM (Document Object Model) has finished loading. It’s a jQuery shorthand for $(document).ready(function() { ... });.
  3. Event Binding:

    • $('.button').on('click', function(event) { ... });: This line selects all elements with the class button using the jQuery selector $('.button'), and then attaches a click event handler to them using the .on() method. When any of these elements are clicked, the function inside the second parameter will be executed.
  4. Prevent Default Action:

    • event.preventDefault();: Inside the click event handler, this line prevents the default action of the event from occurring. In this case, it prevents the default action of clicking a button, which is to submit a form or navigate to another page; this will reload the page and automatically scrolls it to the top.
  5. Changing CSS Properties:

    • $('.target-element').css({ ... });: This line selects all elements with the class target-element and changes their CSS properties using the .css() method.
    • background-color: '#093061': Sets the background color of the selected elements to #093061.
    • color: '#FAFAFA': Sets the text color of the selected elements to #FAFAFA.


In summary, when any element with the class button is clicked, the script prevents the default action, and then changes the background color and text color of all elements with the class target-element. This allows you to change the appearance of the targeted element on your webpage when a specific button is clicked. As mentioned before, this isn’t limited to just colors. As a final tip, I found ChatGPT to be an excellent source to find out what is possible, when you have an already working script.

Florius

Hi, welcome to my website. I am writing about my previous studies, work & research related topics and other interests. I hope you enjoy reading it and that you learned something new.

More Posts

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.