Preview CSS changes in real-time by editing through a form

Is it possible to customize the background color and border weight using input fields in my script?

I'm not very familiar with JavaScript, so if you could provide an example on JSFiddle, I would greatly appreciate it.

$('.divSpecific input').click(function(){
   
    if($(this).attr("alt") == "1")
        $('div.signUp').css("background","red").css("color","black");
            
    else if ($(this).attr("alt") == "2")
        $('div.signUp').css("background","yellow").css("color","black");
    else
        $('div.signUp').css("background","cyan").css("color","white");
  });
div.divSpecific {
    height: 20px;
}
.signUp{
    border:solid 1px;
    height:20px;
    width:100px;
    text-align:center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div>First step - choose button</div>
<div class="divSpecific">Design - ver.1<input type="radio" alt="1" name="name" /></div>
<div class="divSpecific">Design - ver.2<input type="radio" alt="2" name="name" /></div>
<div class="divSpecific">Design - ver.3<input type="radio" alt="3" name="name" /></div>

<br>
  
<div>Or change background, text, and border colors manually (HEX format)</div>
<div>Background color<input type="text"><input type="submit" value="Confirm">
<div>Text color<input type="text"><input type="submit" value="Confirm"></div>
<div>Border color <input type="text"><input type="submit" value="Confirm"></div>

<br>

<div>Adjust border weight</div>
<div>Border weight (default 1 px)<input type="text"><input type="submit" value="Confirm">

<br><br>

<div class="signUp">Sign Up</div>

Answer №1

Develop a function to manage the application of CSS styles and then introduce some data attributes to the radio inputs for setting default values in text areas.

$('[type="radio"]').click(function() {
    $("#border_color").val($(this).attr("data-border-color")),
    $("#border_width").val($(this).attr("data-border-width")),
    $("#bg_color").val($(this).attr("data-bg-color")),
    $("#text_color").val($(this).attr("data-color")),
    applyStyles();
})$('[type="submit"]').click(function() {
    applyStyles();
})
  
function applyStyles() {
    $('div.signUp').css({
        borderColor: $("#border_color").val(),
        borderWidth: $("#border_width").val(),
        backgroundColor: $("#bg_color").val(),
        color: $("#text_color").val(),
    })
}
div.divSpecific {
    height: 20px;
}
.signUp{
    border:solid 1px;
    height:20px;
    width:100px;
    text-align:center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<div>First step - choose button</div>

<div class="divSpecific">Design - ver.1<input type="radio" data-bg-color="red" data-border-color="black" data-color="black" data-border-width="2" alt="1" name="name" /></div>
<div class="divSpecific">Design - ver.2<input type="radio" data-bg-color="yellow" data-border-color="black" data-color="black" data-border-width="2"  alt="2" name="name" /></div>
<div class="divSpecific">Design - ver.3<input type="radio" data-bg-color="cyan" data-border-color="black" data-color="white" data-border-width="2"  alt="3" name="name" /></div><br/>
  
<div>Or change manually background, text abd border (HEX format)</div>
<div>Background color<input id="bg_color" type="text"><input type="submit" value="Confirm"></div><!-- you were missing a close div here -->
<div>Text color<input id="text_color" type="text"><input type="submit" value="Confirm"></div>
<div>Border color <input id="border_color" type="text"><input type="submit" value="Confirm"></div><br>

<div>And change border weight</div>
<div>Border weight (defoult 1 px)<input id="border_width" type="text"><input type="submit" value="Confirm"></div><!-- ...and here -->

<br><br>

<div class="signUp">Sign Up</div>

Answer №2

It appears that instead of clicking, you may want to consider using the change event instead.

Answer №3

To enhance user experience, consider implementing an onClick function for each button:

<input type="text" id="backgroundInput"><button onClick="updateBackground()">Confirm</button>

function updateBackground() {
    $('div.signUp').css('background-color', $('#backgroundInput');
}

Repeat this process with the remaining inputs for consistency.

Alternatively, assign unique IDs to the buttons and create a .click(function()...) handler similar to the approach used for radio buttons.

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

The Element should take up the entire page with the exception of a 20px margin

I am struggling to create an element that covers the entire page except for a 20px margin on all sides. I have tried implementing a solution using this code, which works in webkit browsers and Firefox but seems to have issues with Internet Explorer (10) an ...

Angular JS: By utilizing the ng-if directive, I aim to dynamically update the status from active to inactive

Using Angular's ng-repeat, I have a setup to display admin data. tr(data-ng-repeat="admin in admins") td {{$index+1}} td {{admin.profile.AdminName}} td(ng-if = "admin.account.status === 1") active ...

Is it possible to adjust the width of a parent element based on the number of child

In this particular example, I have structured a header with a logo-container positioned on the left side, a menu in the center, and a button on the right. The menu consists of 5 top-level items and 2 sub-menus. <div class="container"> <div cla ...

JavaScript method overloading involves defining multiple functions with the same name

In my JavaScript code, I have implemented method overloading using the following approach: function somefunction() { //1st function } function somefunction(a) { //2nd function } function somefunction(a,b) { //3rd function } somefunction(); // ...

Unable to modify the color of the bootstrap datepicker

I have implemented a date input using bootstrap-datepicker to provide a calendar for users to select dates. However, I am facing difficulty in changing the background and head's color. Below is my HTML code: <div class="row mb-1"> < ...

Contingent upon the prop in Vue 2, determine whether to render the slot with a wrapper or

My component is simple - it just adds styling to a slot. The styling part is straightforward: it adds padding, margin, border, and background color based on the props passed. Right now, there is a wrapper component with a default slot inside. We bind class ...

Clicking on navigation does not close Bootstrap dropdown

When I use this code on my HTML page, the dropdown menu appears correctly. However, once I click a link, the menu does not close and remains open. How can I resolve this issue? <div class="col-lg-8 col-md-8 col-xs-2 accordion-menu"> <button t ...

Customizing Your WordPress Menu with HTML5 Features

Can someone assist me in creating a customized WordPress menu? I am facing issues with unnecessary classes added by WordPress and the lack of certain attributes on dropdowns. Below is the HTML code: <nav class="navbar navbar-default navbar-static- ...

Configuring vue-jest: Does anyone know how to set up aliases for template/script src attributes in Vue?

Dependencies: "@vue/cli-plugin-unit-jest": "^4.5.13", "@vue/test-utils": "^1.2.1", "vue-jest": "^3.0.7" I am dealing with an application that utilizes an alias (referred to as "foo") de ...

What is the best way to modify a variable in a nested element using ng-click in the parent element?

Review this code snippet: <section class="page-section about-us" scroll-bookmark="about-us" ng-click="activeSection=true" ng-init="activeSection=false"> <div class="page-content sub-content active-section">{{activeSection}} < ...

Adjust the dimensions of the bootstrap dropdown to match the dimensions of its textbox

The second textbox features a bootstrap dropdown with extensive content that is overflowing and extending to other textboxes below it. I am looking for a way to resize the dropdown to fit the size of its corresponding textbox. UPDATE: I want the dropdown ...

Leveraging Google Sheets as a Dynamic Database with Ajax

I found a helpful guide on using Google Sheets as a database with Apps Script and ajax. Here is the link. I understand that I need to include 'Google Sheet/Apps Script Code' in a spreadsheet. However, I'm unsure about what needs to be place ...

An error in Webpack prevents it from resolving the data:text import

I built an app that relies on a third-party library with the following syntax: const module = await import(`data:text/javascript;charset=utf-8,${content}`); While using Webpack to build the app, I encountered this error: ERROR in ./node_modules/@web/test- ...

Having trouble with defining, utilizing functions intended for use within a jquery plugin

Currently in the process of developing my first jQuery plugin, I have successfully completed the initial coding and am now seeking to streamline the code by organizing certain sections into functions. These functions are intended to remain internal to the ...

What is the process of JavaScript loading external libraries?

Can anyone explain how JavaScript manages to load external libraries? Is it simply sending a GET request to the URL specified in the script tags? And where does the browser store these libraries - is it kept somewhere in the DOM? I'm concerned about ...

Pop-up with a unique MediaBox design

Last week, I inquired about window pop-ups and have been brainstorming alternatives. One idea I had is to use mediabox/lightbox style pop-ups. Would it be feasible to create a link that opens a mediabox window off to the side when clicked? Users could dra ...

Boxes rest gently against each other

Hello everyone! I'm currently working on a website for one of my college projects, and I could really use some assistance. I seem to have two boxes touching each other when I actually want a small gap between them. Any help or suggestions would be gre ...

Defining TypeScript type annotations: the art of declaring class constructors

I've developed my own JavaScript library (consisting of a single js file) with multiple classes. To incorporate it into my TypeScript projects, I created a d.ts file containing type definitions for each class. An example of the definition would be: ex ...

Tips for utilizing the <Trans /> component in react-i18next to translate image alt text

Trying to solve a simple issue here. I'm using react-i18next in my project and everything is working smoothly. I can implement translations in both Functional components and classes, but I'm facing a problem with a class component when it comes t ...

Best practices for handling errors with the async pipe

When it comes to async pipe error handling, is there a best practice that you follow? Should errors be handled in the service (even if it requires using global error handling) or is it more appropriate to handle them in the component? What approach do you ...