Customizing your buttons in Wordpress with extra CSS styles upon clicking

I am in the process of developing a Wordpress website and I require a button for switching between mobile and desktop versions manually.

Within my .css file, I have included:

    @media (pointer:coarse) {}

This adds modifications for the mobile version based on whether the device has a cursor or not.

My goal now is to trigger these modifications manually with something like this:

    $('#Button').click(function(){
    ...
    });

The challenge lies in targeting large portions of a .css file.

If you have any alternative solutions, please share! Feel free to ask if anything is unclear.

EDIT: Credit goes to Jignesh Sanghani for the inspiration! I have added a class (.mobile) before every element that I want to activate in mobile mode. In a separate .js file, I used the following code along with the HTML markup:

    <button id="mobileversion">Mobile-Version</button>
    <button id="desktopversion">Desktop-Version</button>

The .js code is as follows:

jQuery(document).ready(function($){

$("body").removeClass("mobile");

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
  $("body").addClass("mobile");
var link = document.getElementById('desktopversion');
  link.style.display = 'block';
}

$('#mobileversion').click(function(){
$("body").addClass("mobile");
var link = document.getElementById('mobileversion');
  link.style.display = 'none';
var link = document.getElementById('desktopversion');
  link.style.display = 'block';
});

$('#desktopversion').click(function(){
$("body").removeClass("mobile");
var link = document.getElementById('desktopversion');
  link.style.display = 'none';
var link = document.getElementById('mobileversion');
  link.style.display = 'block';
});

    });

I now have 2 buttons for switching between versions and can hide one button using .css code to display only one per version of the website.

Answer №1

In my understanding, the most effective approach would involve implementing two separate sets of media queries that are conditionally applied based on the presence of a parent class.

$('#Button').click(function(){
$("body").addClass("w600").removeClass("w768");
});
@media (max-width: 600px) {
.w600 .myDiv {
color: red;
}
.w600 .myOtherDiv {
color: blue;
}
}
@media (max-width: 768) {
.w768 .myDiv {
color: red;
}
.w768 .myOtherDiv {
color: blue;
}
}

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

How to align the "bootstrap navbar li items" in the center across all page widths

I'm trying to center the li items "on the entire page width" in this basic bootstrap navbar that I've shared. I attempted using "justify-content-center" in the parent. It did center the navbar items, but not across the entire page width. The ite ...

Find the element that contains a specific substring in its value using JavaScript

I am trying to find a way to count how many input fields with the class name "text" contain a specific value. document.getElementById('c_files').getElementsByClassName('text').length; Currently, this code counts all textboxes with the ...

Tips for ensuring elements within a modal receive immediate focus when opened in Angular 2

I am relatively new to Angular JS and I am encountering some challenges with implementing a directive in Angular 2 that can manage focusing on the modal when it is opened by clicking a button. There have been similar queries in the past, with solutions pr ...

Choose with text partially aligned on the right side

Is there a way to align a portion of the option's text to the right? Below, you will see a select element with names on the left and "(vertical)" on the right. I want to move "(vertical)" to the right side. Is it possible to achieve this? https://i. ...

What is the method for incorporating opacity into the background color CSS attribute using a Fluent UI color?

Currently, my challenge involves applying opacity to a background color sourced from the fluent UI library, which utilizes Design Tokens. Typically, I would add opacity to a background color like this: background-color: "rgba(255, 255, 255, 0.5)" However ...

JS Function created to supply elements to React component is failing to return correctly

Trying to validate a dataset by checking for specific prefixes or suffixes in a string, and then breaking the string into <span> elements. The current function correctly identifies the relevant morphemes in the data set, but fails to return the split ...

Creating a link using curly braces {{ }} in VUE js

I need assistance in creating links to different pages using data in curly brackets {{ }}, I am having trouble figuring out the correct way to implement them. <div id="app"> <div v-for="data in categorie"> &l ...

What is the best way to redirect a URL parameter that contains "?m=1"?

After transitioning from blogger to wordpress, I've noticed that many incoming links now have a "?m=1" parameter added to the end of my post link. For example: http://www.example.com/2015/06/name-of-blog-post.html?m=1 I've tried searching for ...

Setting up and using npm jshint with Grunt and Node.js

I have successfully installed node.js on Windows with the npm package. My project is located in the D drive at D:>projectD I am currently working on running jshint along with SASS, concat, etc. Everything seems to be working fine except for jshint ...

React - Incorporating Axios catch errors directly within form components

When a user registers, my backend checks if the email and/or username provided are already in use by another user. The response from Axios catch error is logged into the web console. I want to associate each email and username with their respective fields ...

What could be the reason behind the malfunctioning of my media query in the

I am trying to connect an external stylesheet to my React component in order to apply different styles based on screen width. Specifically, when the screen width is less than 300px, I want the logo to have a height of 100vh. However, it seems that the medi ...

Outlook failing to recognize text dimensions

Implementing: <html> <head> <style> p { font-size: 16px; } .font-size-16 { font-size: 16px !important; } </style> </head> <body> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspend ...

Disabling an image is similar to deactivating a button

document.getElementById("buttonName").disabled = true; I have successfully disabled a button using the code above. However, I am now facing a challenge with using pictures as buttons that have an onClick function. Is there a way to disable the onClick fun ...

Namespace remains ambiguous following compilation

I'm currently developing a game engine in TypeScript, but I encountered an issue when compiling it to JavaScript. Surprisingly, the compilation process itself did not throw any errors. The problem arises in my main entry file (main.ts) with these ini ...

Exploring nested arrays of objects and applying value constraints

Is there a way to iterate through an array and display only 5 items at once, with the option to call a function on click that will add another 20 items? I have an object structured like this: let someObject = [ { data: [ { id: 123, ...

How is the purpose of nesting functions within functions achieved in nodejs?

Take a look at this example: var tools1 = require('../tools/tools1'); var test_func = function(arg1, arg2, arg3) { var local_var_1 = "lc1"; var local_var_2 = "lc2"; return function(data) { var result = tools1.doSth(local_va ...

What is the best way to open a new window, such as a music player, and integrate it into a Shopify environment

Currently, within my Shopify store, I am using the following code: <script language="javascript" type="text/javascript"> function popupwindow(url, winName, w, h) { var left = (screen.width/2)-(w/2); var top = (screen.height/2)-(h/2); return wi ...

What is the most effective way to loop through an object containing DOM selectors as values, and subsequently utilize them to assign new values?

I have a function that takes an object retrieved from a database as its argument. My goal is to display the values of this object in a form by associating each value with a specific DOM selector. Here is the code snippet where I achieve this: function pai ...

Extending fabric.js toObject with custom properties can result in the loss of default properties

After doing extensive research on various platforms, including this one, I am still struggling to get everything working as desired. My main goal is to save custom properties in all shapes, specifically the uuid and rt_attributes. Following the manual, I ...

Preventing default behaviors in VueJS textarea components

I'm attempting to incorporate a slack-like feature that sends a message only when the exact Enter key is pressed (without holding down the Shift key). Looking at this Vue template <textarea type="text" v-model="message" @keyup.enter.exact="sendMe ...