1

This a sample of the script I have so far. The default behavior once an <li> is selected, is to change the background color to orange (also default). My goal is to have every <li> changing to a different color when selected.

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

 <style>
.st0{fill:#FFFFFF;stroke:#000000;stroke-miterlimit:10;}
#selectable .ui-selecting { background: #FECA40; }
#selectable .ui-selected { background: #F39814; color: white; }
#selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; width: 85px;}
</style>

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
  $( "#selectable" ).selectable();
});

$( "#btn1" ).click( function(event, ui) {
  // code here is too long to include
} ); 
</script>
</head>
<body>

<ol id = "selectable">
    <li id = "btn1" class="ui-widget-content">Office #1</li> 
    <li id = "btn2" class="ui-widget-content">Office #2</li> 
    <li id = "btn3" class="ui-widget-content">Office #3</li> 
    <li id = "btn4" class="ui-widget-content">Office #4</li>     
</ol>
</body>
</html>
3
  • used "<li>" tags in the question which automatically turned to bullet points. Apologies for the rookie mistake. I'm new to programming and this is my first post! Commented Aug 3, 2017 at 23:59
  • Do you have specific colors in mind and will you always only have four buttons? Commented Aug 4, 2017 at 0:02
  • Specific colors don't matter. As long as each button has a unique color. Commented Aug 4, 2017 at 0:16

2 Answers 2

3

What about getting a random number (between 0 to 255) to define a RGB color?
It doesn't ensure the color will be unique... But...

$(document).ready(function(){

  $( "#selectable" ).selectable();

  $("li").on("mousedown",function(){
    var r = parseInt(Math.random()*255);
    var g = parseInt(Math.random()*255);
    var b = parseInt(Math.random()*255);

    console.log("rgb("+r+","+g+","+b+")");
    $(this).css({"background-color":"rgb("+r+","+g+","+b+")"})
  });

});
.st0{fill:#FFFFFF;stroke:#000000;stroke-miterlimit:10;}
#selectable .ui-selecting { background: #FECA40; }
#selectable .ui-selected { background: #F39814; color: white; }
#selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; width: 85px;}
<link href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<ol id = "selectable">
  <li id = "btn1" class="ui-widget-content">Office #1</li> 
  <li id = "btn2" class="ui-widget-content">Office #2</li> 
  <li id = "btn3" class="ui-widget-content">Office #3</li> 
  <li id = "btn4" class="ui-widget-content">Office #4</li>     
</ol>

By the way, the click event is prevented by .selectable(), so I used mousedown.

Sign up to request clarification or add additional context in comments.

6 Comments

That's great, but how do you revert the button back to original state?
I was just curious, I'm gonna give it a try, but I'm sure I will fail lol..
@almostabeginner: You could add a "reset" button... And set all li background-color to initial... Like $("#reset").on("click", function(){$("li").css({"background-color":"initial"});)
I meant when button1 is clicked -> selected -> selected color is applied to button1, but when button2 is clicked, then button1 shouldn't continue to have selected color applied to it. It should revert back to original non-selected color, which is white.
$("li").css({"background-color":"initial"}); resets all li colors to initial... So use it right before $(this).css({"background-color":"rgb("+r+","+g+","+b+")"}); for the behavior you ask for.
|
0

Since all the <li> elements share a class, you could just target that class and use .addClass() to give them the .ui-selected class.

$('.ui-widget-content').addClass('.ui-selected');

1 Comment

If I go with the class solution, how would I set each to a different color? How would that work when unselecting/selecting a different button? Does it revert to it's original state?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.