Getting Color With Name Using JSON - Stack Overflow
Có thể bạn quan tâm
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams - Teams
-
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 CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet 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 1Im 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ł LipaMichał Lipa 1179 bronze badges Add a comment |2 Answers
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 1Try 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 HackermanHackerman 12.3k2 gold badges36 silver badges46 bronze badges Add a comment | 1In 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 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 discardedSign up or log in
Sign up using Google Sign up using Email and Password SubmitPost as a guest
Name EmailRequired, but never shown
Post Your Answer DiscardBy 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
Related
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 valueHot 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?
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
lang-jsTừ khóa » Html Color Names Json
-
140 Html Color Names As An Array Of Json Objects—enjoy!
-
Color-names/color-names.json At Master · Jonathantneal ... - GitHub
-
Css-color-names/css-color-names.json At Master - GitHub
-
HTML Color Names - W3Schools
-
HTML Color Groups - W3Schools
-
Theme.json Color Options - Full Site Editing
-
HTML Color Names - GeeksforGeeks
-
HTML Color Names
-
Example Colors JSON File - SitePoint
-
Color-name-list V9.17.0
-
Json Colors Other Than The .mscolors - Microsoft Tech Community
-
Javascript: How To Get List Of All HTML Color Names
-
Color Chart With 140 HTML Color Names
-
Best HEX To Color Name Converter