How do I let a user click a color to become a function that changes the fill of an svg object on click

Thomas

I'm trying to create a color pallette that users can click on that then becomes the color to fill an svg object. kind of like a simple coloring pages for kids. I can't seem to figure out how to tell javascript to save the color, and I can't use php.

I've tried to create an input type color. The problem is the transition between chosing the color and clicking the color

I think my code is not even the direction I should be going..

<input type="color" id="myColor">
<input type="color" id="myColor">
<button class="btn" id="btn-reset">reset</button>

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="500px" height="500px" viewBox="0 0 500 500" enable-background="new 0 0 500 500" xml:space="preserve">

<path id="path1" fill="#FFFFFF" stroke="#231F20" stroke-miterlimit="10" d="M291.451,51.919v202.54c0,0,164.521,119.846,140.146,0
    C407.227,134.613,291.451,51.919,291.451,51.919z"/>

<path id="path2" fill="#FFFFFF" stroke="#231F20" stroke-miterlimit="10" d="M169.595,150.844c0,0-76.24,69.615-40.606,128.309
    c35.634,58.695,155.798-51.867,151.654-85.993C276.498,159.034,177.054,89.42,169.595,150.844z"/>

<path id="path3" fill="#FFFFFF" stroke="#231F20" stroke-miterlimit="10" d="M40.332,90.466c0,0-39.911,76.119-2.691,87.83
    c37.22,11.71,78.923-46.844,56.054-78.462C70.826,68.216,40.332,90.466,40.332,90.466z"/>
</svg>

</div>

  var x = document.getElementById("myColor").value;
  document.getElementById("demo").innerHTML = x;
}

I expect the colors at the top to save their color code to fill the paths in the svg onclick. But I can't get it to work. Like a coloring page for kids.

Mister Jojo

ID must be unique !

const
      ChoseColor   = document.querySelector('#btn-Color-hidden'),
      ResetButton  = document.querySelector('#btn-reset'),
      All_svgPath  = document.querySelectorAll('#Layer_1 path'),
      defaultColor = '#FFFFFF',
      LayerSVG     = document.querySelector('#Layer_1');

    var PathColor = null;

    LayerSVG.onclick=e=>{
      if (!e.target.matches('path')) return;
      e.stopPropagation();
      PathColor = e.target;
      ChoseColor.value = PathColor.getAttribute('fill');
      ChoseColor.click();
    }

    ChoseColor.onchange=_=> {
      PathColor.setAttribute('fill', ChoseColor.value)
    }

    ResetButton.onclick=_=>{
      All_svgPath.forEach(p=>{ p.setAttribute('fill', defaultColor) })
    }
<input type="color" id="btn-Color-hidden" style="display: none">

  <button class="btn" id="btn-reset">reset</button>
  
  <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
  width="500px" height="500px" viewBox="0 0 500 500" enable-background="new 0 0 500 500" xml:space="preserve">
  
  <path id="path1" fill="#FFFFFF" stroke="#231F20" stroke-miterlimit="10" d="M291.451,51.919v202.54c0,0,164.521,119.846,140.146,0
      C407.227,134.613,291.451,51.919,291.451,51.919z"/>
  
  <path id="path2" fill="#FFFFFF" stroke="#231F20" stroke-miterlimit="10" d="M169.595,150.844c0,0-76.24,69.615-40.606,128.309
      c35.634,58.695,155.798-51.867,151.654-85.993C276.498,159.034,177.054,89.42,169.595,150.844z"/>
  
  <path id="path3" fill="#FFFFFF" stroke="#231F20" stroke-miterlimit="10" d="M40.332,90.466c0,0-39.911,76.119-2.691,87.83
      c37.22,11.71,78.923-46.844,56.054-78.462C70.826,68.216,40.332,90.466,40.332,90.466z"/>
  </svg>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SVG - Change fill color on button click

How do I create a button that changes text and color on click in Google Sheets App Scripts / Drawing?

Function in HTML to grab the object (SVG) where I click?

how do i set fill color of a svg circle with the help of thymeleaf

Django: How can I create a dynamic form that changes on user click?

How do i use a click function to undo a click event?

How do I make it so that when I click it changes the list

How do I use the Selenium Click function

How do I fill the button background with a circle on click

How to do a click function

How to create a button on click the background color changes

java null object reference error / I want my TextView changes color and text when I click the button

How can I let the user fill in an array?

How do I test a click on SVG objects using Selenium webdriver?

Flutter - How do I toggle the color of a RaisedButton upon click?

How do I make whole table clickable and add color on click?

How do I change the background of a paragraph to a random color on click with jQuery?

How do I change the color of a stroke using the click of a button in Javascript?

How do I Click a JButton without the user Clicking it?

How do I let TS compiler know a function exist in an object?

How do I let python know that I click "search" button for aspx website?

How to change the color of an object created with JSONLoader on click

SVG - click is not a function

How can i create a template for the user to fill in, after they click on a button on top of an image?

How do I slow down the execution of a jquery click function?

How do I call a Django function on button click?

How do I call a Django function on button click?

How do I pass data into a click() function when manually call it?

How do I make a function that reset the game on click