Can someone provide guidance on incorporating a checkbox into my WordPress site?

I've created a toggle switch and now I want to incorporate it into my website, but I'm unsure of how to do so. The site uses the bbPress plugin on WordPress, and I would like users to be able to click the toggle switch when leaving a comment, with their input then being displayed in the comment.

Here is the HTML for the toggle switch:

<!-- ____[TOGGLE BUTTON]____ -->
<div class="toggle">

    <!-- ____[OUTER BOX WITH BORDER LINE]____ -->
    <div class="toggle__box">

        <!-- ____[INPUT WITHOUT VISIBILITY LINKED WITH LABEL]____ -->
        <input type="checkbox" id="toggle-check">

        <!-- ____[TOGGLE LABEL FOR INPUT]____ -->
        <label for="toggle-check" class="toggle__label">

            <!-- ____[TOGGLE THUMB ICON]____ -->
            <div class="toggle__thumb">
                <img src= <?php get_image_for_slider(); ?> >
            </div>

            <!-- ____[TEXT OF TOGGLE]____ -->
            <div class="toggle__text--green">BULLISH</div>
            <div class="toggle__text--red">BEARISH</div>
        </label>

    </div>

</div>
`

And here is the CSS for this button: *, *::before, *::after { margin: 0; padding: 0; -webkit-box-sizing: border-box; box-sizing: border-box; }

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.toggle {
  width: 100%;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  align-items: center;
  justify-content: center;
  ;
}
.toggle input {
  display: none;
  opacity: 0;
}
.toggle__box {
  width: 13rem;
  height: 4rem;
  border-radius: 100px;
  border: 1px solid #707070;
  position: relative;
  overflow: hidden;
}
.toggle__label {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  -webkit-transition: all .3s linear;
  transition: all .3s linear;
  cursor: pointer;
}
.toggle__thumb {
  width: 3rem;
  height: 3rem;
  position: absolute;
  top: 50%;
  left: .6rem;
  z-index: 2;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
  -webkit-transition: all .3s linear;
  transition: all .3s linear;
}

.toggle__thumb img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  -o-object-fit: cover;
  object-fit: cover;
}

.toggle__text--red,
.toggle__text--green {
  position: absolute;
  top: 50%;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
  font-size: 1.6rem;
  font-weight: 500;
  -webkit-transition: all .005s linear;
  transition: all .005s linear;
  z-index: 0;
}

.toggle__text--red {
  color: #FF001A;
  left: 10%;
  opacity: 0;
  visibility: hidden;
}

.toggle__text--green {
  right: 10%;
  color: #29FF00;
}

.toggle #toggle-check:checked+label .toggle__thumb {
  left: calc(100% - .6rem);
  transform: translate(-100%, -50%);
}

.toggle #toggle-check:checked+label .toggle__text--red {
  opacity: 1;
  visibility: visible;
}

.toggle #toggle-check:checked+label .toggle__text--green {
  opacity: 0;
  visibility: hidden;
}` 

Is there a way to link a variable to this switch/checkbox so that when the user clicks it, I can use that to display something on the screen as described above? I attempted using isset($_POST["name"], but it didn't work. I've heard that adding an event listener in JavaScript might work, but I am unfamiliar with coding in JavaScript. Any help would be greatly appreciated!

Thank you

Answer №1

If you're looking to tackle this problem with JavaScript, you're in luck! Simply include the following JS code and customize the function as needed:

document.getElementById("toggle-check").addEventListener("click", customFunction);

function customFunction() {
  /* Add your desired functionality here */
  document.getElementById('p').innerHTML = "Clicking the box will display this text!";
}
body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* CSS styles for the toggle switch */

.toggle {
  width: 100%;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Additional CSS styling goes here */

<input type="checkbox" class="toggle" id="toggle-check">
<p id="p"></p>

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

Issues with Grunt functionality after installation on Ubuntu

I successfully installed Grunt by running the following commands in the terminal: sudo apt-get install nodejs sudo apt-get install npm npm install -g grunt-cli After executing npm install -g grunt-cli, here is the output from the terminal: (output he ...

Using Angular JS version 1.2.26 to implement promises within a forEach iteration

I am working on a JavaScript project where I have implemented an angular.forEach loop to iterate over image configuration objects and create Image() objects using the URLs from the config. My goal is to ensure that none of the resulting images are returne ...

What is the best way to paste plain text into a Textarea without any formatting?

A challenge I am facing in my project is related to copying and pasting text into a Textarea. When a user copies text, the style of the text is also inserted along with it. However, when the user manually types the text, it gets inserted correctly without ...

Getting the toState parameter using the $transitions service in the latest version of the ui-router

In my project, I am currently utilizing ui-router version 1.0.0-alpha.3. Please note that the older events have been deprecated in this version. As a result, I am in the process of migrating from: $rootScope.$on('$stateChangeStart', (event, toS ...

What is the best way to superimpose text on a variety of images with varying sizes using CSS

I have a client who is requesting a sold text to be displayed on top of all images that have been sold, positioned in the bottom left corner of each image. While I have successfully implemented this feature, the issue arises when dealing with images of var ...

Change the source of another element when clicked

Check out the fixed version of previously broken code here I am facing challenges in changing an attribute on another element from a click function sniping $('#black').click(function() { $('#blackbox').slideToggle('slow&apos ...

The compilation of the Angular application is successful, however, errors are arising stating that the property does not exist with the 'ng build --prod' command

When compiling the Angular app, it is successful but encountered errors in 'ng build --prod' ERROR in src\app\header\header.component.html(31,124): : Property 'searchText' does not exist on type 'HeaderComponent&apo ...

What could be the reason for the lack of error handling in the asynchronous function?

const promiseAllAsyncAwait = async function() { if (!arguments.length) { return null; } let args = arguments; if (args.length === 1 && Array.isArray(args[0])) { args = args[0]; } const total = args.length; const result = []; for (le ...

Designing a unique CSS border for custom list item bullets

I'm currently exploring the possibility of implementing a CSS border around an image that I have imported as a custom bullet for my list items: ul { list-style: none; margin-right: 0; padding-left: 0; list-style-position: inside; } ul > ...

The font rendering in Safari appears to have distinct differences compared to other web browsers

My concern is related to the HTML tag below: <a href="/partner-team/team" class="button button__style--1">MEHR</a> This is how the CSS appears: a { text-decoration: none; } .button { padding: 0.2rem 4rem; curso ...

steps to remove the border of the select box in MUI component

i am struggling to disable the border on the select box and change the color of the label text when focused or blurred i have attempted it but have been unsuccessful i am not very skilled at Mui component customization this is the image that i desire ht ...

"Unlock the power of Passport.js: A guide to leveraging async rendering for email templates

Currently, my setup involves running Express with Sequelize/MariaDB and Passport.js to handle user authentication. Everything seems to be working smoothly except for the activation email that needs to be sent out after a user signs up. Despite having the ...

How to prevent text from extending beyond the maximum width limit set in HTML

I defined a max-width of 500px for the div element, but the text inside is overflowing the width instead of wrapping to the next line. Can someone please assist with which code needs to be modified? Here is the code snippet in question: The div is set t ...

Only one condition is met when using the Javascript if statement with the :checked and .bind methods

I need help creating an Override Button to disable auto-complete for a form using Javascript. I want the div "manualOverrideWarning" to display a warning if the button is selected, but my current function only works the first time the user presses the butt ...

Issues with CSS causing items to not line up in a single row

I'm currently working on an html/css page and trying to align the items of the agenda class in the same row, centered on the page. https://i.sstatic.net/8TjG8.png Here's the code I've been using: <html lang="en" xmlns="ht ...

Omit components from the function

After clicking a tab, the window automatically scrolls down because of the code below. How can I keep the smooth-scroll effect while excluding specific HTML elements using jQuery selectors? Here is the code in question: //smooth-scrolling $(function() { ...

The z-index property in jQuery dialog does not seem to function properly when used within an

When using bootstrap with jQuery dialog, I encountered a strange issue. If the z-index of the dialog is set to 1 or higher in a standalone script, everything works fine. However, when the same script is called inside an iframe, the dialog ends up appearing ...

What is the standard text displayed in a textarea using jQuery by default

My goal is to display default text in a textarea upon page load. When the user clicks on the textarea, I want the default text to disappear. If the user clicks into the textarea without typing anything and then clicks out of it, I'd like the default t ...

Which is the superior choice: Classic Ajax or jQuery Ajax?

I have a question about using AJAX. I'm stuck between choosing traditional AJAX and jQuery AJAX. When I use jQuery AJAX, sometimes it behaves strangely in Internet Explorer. This is surprising to me because jQuery is supposed to be a cross-browser lib ...

Steps for Creating a Typed Action with No Parameters

There was a suggestion in a Github issue regarding the use of TypedAction.defineWithoutPayload for this specific purpose, but I couldn't locate any relevant examples on how to implement it. I typically utilize this method during login processes where ...