Using HTML and JavaScript, we can set two different color values - one for the background and one for the h1 title

I am trying to save two values, one for the h1 tag and one for the body background. I want the user to select color 1 and color 2. When I press the third button, all changes should be applied and the colors should change. I have attempted this but with no success :(

(html)

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>All the elements</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

    <div class="h1tag">
        <ul>
            <h1> This is a title</h1>
            <li>Paragraph 1: I went on a trip</li>
            <li>Paragraph 2: and I took with me</li>
            <li>Paragraph 3: an extension cord</li>
            <li>Paragraph 4: and a flashlight</li>
        </ul>
    </div>

    <button type="button" onclick="saveUserData()" class="button0">Save Background Color</button>
    <input id="color0" type="color" value="#ff0000">

    <button type="button" onclick="saveUserData()" class="button1">Save Title Color</button>
    <input id="color1" type="color" value="#ff0000">

    <button onclick="applyColors()">Apply Colors to Page</button>

</body>

<footer>
<script defer src="script.js"></script>
</footer>
</html>

(javascript)

function applyColors() { 
    var h1 = document.querySelector(".h1");
    var backgroundColor = document.getElementById("color0").value;

    var h1Tag = document.querySelector(".h1tag");
    var titleColor = document.getElementById("color1").value;


    document.body.style.backgroundColor = backgroundColor + " " + titleColor;
}

Answer №1

You missed the mark by not assigning it to the correct variables.

Find the functioning code below.

function myFunction(){ 
var matches = document.querySelector("h1");
var button0 = document.getElementById("kleur0").value;

//var matches = document.querySelector(".h1tag");
var button1 = document.getElementById("kleur1").value;

 document.body.style.backgroundColor = button0;
matches.style.color = button1;
}
0

I need to save two values one for the h1 tag and one for the body background I let th user select the color 1 and color 2 now when i press on the third button all changes have to be aplied and the color has to change I tryed but with no luck :(

(html)

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>All the elements</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>


    <div class="h1tag">

<ul>
    <h1> dit is een title</h1>
    <li>paragraaf 1: Ik ging op reis</li>
    <li>paragraaf 2: en ik nam mee</li>
    <li>paragraaf 3: een stekkerdoos</li>
    <li>paragraaf 4: en een zaklamp</li>


</ul>
</div>


        <button type="button" onclick="saveUserData()"class="button0">achtergrond kleur opslaan</button>
        <input id="kleur0" type="color" value="#ff0000">

        <button type="button" onclick="saveUserdata()"class="button1">titel kleur opslaan</button>
        <input id="kleur1" type="color" value="#ff0000">

        <button onclick="myFunction()">kleuren toepassen op pagina</button>

    </body>



<footer>
<script defer src="script.js"></script>
</footer>
</html>

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

Unable to add items to the collection in NPM with Meteor 1.3

I have encountered an issue with the imap-simple NPM package while trying to perform an insert operation. Despite following the suggestions on , I am still unable to get the insert function to work properly! Even after simplifying the code and eliminatin ...

I attempted to craft a toggle button by applying and removing an active class that I had previously designed, but unfortunately, it did not function as intended

Every time I click on a button, I keep encountering this error message. I am certain that my selector is correct, but I can't seem to figure out why I'm getting the Uncaught TypeError: Cannot read property 'classList' of undefined at HT ...

What is the best way to allow someone to chain callback methods on my custom jQuery plugin?

My goal is to enhance the functionality of jQuery.post() by implementing a way to check the response from the server and trigger different callbacks based on that response. For instance: $("#frmFoo").postForm("ajax") .start(function () { showSpinner( ...

Is it possible for a method within a class to retrieve properties from a different object within the same class?

I'm facing an issue with accessing the necessary object properties within a method. In my scenario, I have a Game class that generates a new game object. Once the object is created, I attempt to execute the draw method. This draw method requires infor ...

Having issues with Sencha Touch not initiating in mobile browsers

For quite some time now, I have been developing a mobile app using Sencha Touch 2.1 and conducting tests primarily on desktop Chrome and an iOS PhoneGap / Cordova package. Recently, I made the decision to release it as a "native" app while also offering re ...

Get a Blob as a PNG File

Hope you had a wonderful Christmas holiday! Just to clarify, I am new to JS and may make some unconventional attempts in trying to download my Blob in PNG format. I am facing an issue with exporting all the visual content of a DIV in either PDF or image ...

Utilizing an Angular foreach loop for restructuring JSON data

I currently have an ng-repeat function that outputs arrays of objects in the following format: [ {"day":"10","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"}, {"day":"3","title":"day","summary":"summary","description" ...

Tips for making a multi-dimensional array using jQuery

Is it possible to generate a jQuery layout by using two separate each statements as shown below? arrar [ 'aaa'=>'ccsdfccc', 'bb'=>'aaddsaaaa', '1'=>[ 'three'=>'sdsds& ...

module 'next/router' cannot be located or its associated type declarations are missing

Running into some issues with my NextJS application. An unusual error message is appearing, even though my code is functioning smoothly without any errors. import { useRouter } from 'next/router'; // Cannot find module 'next/router' or ...

Align title text to the right within the Material Design Card

I have implemented a Material Design (MDL) card to showcase numeric values in the title. However, I am facing an issue where the values are left aligned by default and I want them to be right aligned. Despite trying various styles, they are being ignored ...

Discover the secret to restricting JSON API functionality in your WordPress plugin

I am interested in using WordPress to build a website, specifically looking to export site posts using the JSON API. However, I encountered an issue when attempting to limit the displayed posts by category. Clicking on the "get_category_posts" link within ...

What is the best way to limit the number of items shown in a cart dropdown using JavaScript

I have a shopping cart feature added to my ecommerce web app. Within the header, there is an icon of a cart. When clicked, a dropdown appears showing the items added to the cart. However, if I have 10 items in the cart, the dropdown becomes too lengthy to ...

Enlarge an element to the extent of filling the list item

I am facing a challenge in expanding the a elements within this jsfiddle to fully occupy the li element. The use of display:block does not seem to be effective since I am utilizing a table for spacing. What steps can I take to achieve this while ensuring ...

Troubles with horizontal list incompatibility in Internet Explorer 6 and 7

I am facing an issue with displaying an unordered list in IE6+7. The problem arises when styling the list items with specific width and height properties, causing IE to stack the items vertically instead of horizontally. Below is my code snippet: For live ...

Unspecified OrbitControls Compatibility Issue between Angular2 and Three.js

I'm running into issues trying to set up OrbitControls in my Angular2 project. I managed to display a scene with a box, but I'm struggling to move the camera. Upon investigation, I found that my OrbitComponent, responsible for defining orbit con ...

How can I efficiently retrieve the server time in JavaScript using a web browser?

Is there a faster way to synchronize client-side time with server time in JavaScript? I found this solution on Stack Overflow, which seems good, but are there any other ideas that might be quicker? ...

Angular factory transforming service

I am looking to transform the 'i18n' function into a factory in order to return a value instead of just setting it. Any suggestions or tips would be greatly appreciated! services.factory('i18nFactory', function() { var language = ...

Examine the spans and delete the first-child node

Basically, this functionality allows the user to cycle through hidden information by clicking on it. Here is how you can structure your HTML: <div id="facts"> <span>click to cycle</span> <span>fact 1</span> <s ...

Challenges with line height in IE when adjusting font size in textarea

I'm facing an issue with adjusting the font size in a textarea using JavaScript. While it works perfectly in Firefox, there are some problems in IE. Specifically, the line-height does not change accordingly. This results in gaps between lines when the ...

What are the negative effects of placing an external CSS stylesheet link outside of the <head>

At the outset, it is well-known that the <link> tag used to connect an external CSS style sheet should ideally be placed within the <head> section of an HTML document. It is considered unconventional to place it elsewhere. However, due to my u ...