Getting Color With Name Using JSON - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs Getting color with name using JSON Ask Question Asked 10 years, 1 month ago Modified 10 years, 1 month ago Viewed 2k times 1

Im working on color quiz game. I would like to get random color + this color name from JSON.

My JSON example:

{ "color":"#72362a", "name":"green", "namePL":["zielony"]}, }, { "color":"#7a3543", "name":"yellow", "namePL":["żółty"]}, } ];

How I can set body background to random from JSON and get name of used color?

Found random color code but I want to get color from JSON.

RandomColor = function() { colors = ['red', 'white', 'blue', 'green'] return colors[Math.floor(Math.random()*colors.length)]; }

What should I modify to get what I want?

I want to change color of background and get name of this color:

$('#colorname').text(name); Share Improve this question Follow asked Nov 4, 2014 at 18:00 Michał Lipa's user avatar Michał LipaMichał Lipa 1179 bronze badges Add a comment |

2 Answers 2

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 1

Try something like this:

var myJson = [{ "color":"#72362a", "name":"green", "namePL":["zielony"]}, { "color":"#7a3543", "name":"yellow", "namePL":["żółty"]}, ] ; var getColour; $(function(){ getColour = function(){ var entry = myJson[Math.floor(Math.random()*myJson.length)]; return entry; } $("#getColour").click(function(){ var localObj = getColour(); $("#divTest").html("Color name: " + localObj.name); $("#divTest").css('background-color', localObj.name ); }); });

Working fiddle: http://jsfiddle.net/robertrozas/vhkdzgo2/

Share Improve this answer Follow answered Nov 4, 2014 at 18:16 Hackerman's user avatar HackermanHackerman 12.3k2 gold badges36 silver badges46 bronze badges Add a comment | 1

In your random color function, you could replace the Array ['red', 'white'..] with your JSON Array. Now the RandomColor object will be a JSON object and you can access the color and name values as you would any JSON object.

RandomColor = function() { colors = [{"color":"#72362a","name":"green","namePL":["zielony"]},{"color":"#7a3543", "name":"yellow", "namePL":["żółty"]}]; return colors[Math.floor(Math.random()*colors.length)]; }

Now you can use RandomColor.color and RandomColor.name to get the values you need.

Share Improve this answer Follow answered Nov 4, 2014 at 18:10 wblanks's user avatar wblankswblanks 59810 silver badges27 bronze badges Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • Why do developers love clean code but hate writing documentation?
  • This developer tool is 40 years old: can it be improved?
  • Featured on Meta
  • The December 2024 Community Asks Sprint has been moved to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries
2 Returning name of colors using jQuery 0 jQuery find similar color from JSON array 1 Getting JSON object values by name 0 Is it possible to return color name with JQuery 0 Change color of a div based on the color value from JSON 0 HTML variable color 0 Jquery change CSS color of several label by name passing through JSON 2 Variable color not changing based on Value 1 Getting specific color for specific value using JSON data 0 How can I change the color in javascript depending of object value

Hot Network Questions

  • Why does ctldl.windowsupdate.com not use (valid) TLS?
  • Is ‘drop by’ formal language?
  • A SAT question about SAT property
  • Best Practices for Managing Open-Source Vulnerabilities in Enterprise Deployments
  • How to add face/ fill edges to inner geometry in geometry nodes?
  • Translation of "Nulla dies sine linea" into English within Context Given
  • How to merge at intersection line point?
  • Why is Jesus called Prince of Peace and not King of Peace considering he was also called Eternal Father?
  • Exploiting MSE of binary data for fast search
  • How to read this old French speed gauge?
  • Left Justified Above Caption in hvfloats
  • Why does a country like Singapore have a lower gini coefficient than France despite France having higher income/wealth taxes?
  • Why is there no AES-512 for CTR & variants to have good large nonces?
  • Merits of `cd && pwd` versus `dirname`
  • More efficient way to color-code cycle permutation list
  • Can I bring candles on an European flight?
  • How did 1977's Car Polo arcade game by Exidy perform hitbox detection, and rigid body collision and movement on the ball?
  • C++20 Robust File Interface
  • Study the convergence of improper integral from 0 to infinity
  • A mistake in revised version
  • Object recognition and barcode scanning with the same camera
  • ping from script launched by cron
  • Will a PC complain if a USB 2 flash drive is powered externally?
  • How feasible would it be to "kill" the Sun by using blood?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-js

Từ khóa » Html Color Names Json