1

In my example, there is two select tag where if I click any option from the first select then it's corresponding options will be shown in second select. My requirement is to set color in the options of all select while user will click(eg: green) on any. And if the user click on any selected option then that color will be gone(white). That means a toggle of color regarding selection(also any appearance of tick mark also acceptable). I have thought it because it will give the user a tracking on the basis of the color while right no there is no opportunity like that.

A combination I am writing here(This toggle color is only for the second select):
If I rewrite the steps(I am thinking to modufy the post by the following lines):
STEP_1: user chosen 0001_Summer and automatically row_heat_0 is selected(I don't know how to disable this. if can disable it also a good leap) and color will be green for this option.

STEP_2: if user again click on row_heat_0 the color will turn to white(that means not selected).

STEP_3: if user now click on row_heat_1 the color will turn to green for this option.

STEP_4: if user now click on row_heat_0 the color will turn to green for this option as well. Now if user select All then that option will be in green color So, in this stage there is 2 option is green in second select and they are row_heat_0, row_heat_1 and All. Also in the first select one option is green and that is 0001_Summer.

STEP_5: if user now change the first select option to 0003_Spring and do some select/not_select operation also same affect will be seen.

STEP_6: if user turn to 0001_Summer still the seleted will be seen in green color(row_heat_0, row_heat_1, All).

I have tried this but failed to see any change.

I am giving teh whole code where I want to apply it

    data = {
  "0001_Summer": {
    "param": [
      "Select_Param",
      "row_heat_0",
      "row_heat_1",
      "row_heat_2",
      "row_heat_3",
      "row_heat_4",
      "row_heat_5",
      "row_heat_6",
      "All"
    ],
    "value": [
      "Select_Param",
      "value_00",
      "value_01",
      "value_02",
      "value_03",
      "value_04",
      "value_05",
      "value_06",
      "All"
    ]
  },
  "0002_Winter": {
    "param": [
      "Select_Param",
      "row_cloud_0",
      "row_cloud_1",
      "row_cloud_2",
      "row_cloud_3",
      "row_cloud_4",
      "row_cloud_5",
      "row_cloud_6",
      "row_cloud_7",
      "row_cloud_8",
      "All"
    ],
    "value": [
      "Select_Param",
      "value_00",
      "value_01",
      "value_02",
      "value_03",
      "value_04",
      "value_05",
      "value_06",
      "value_07",
      "value_08",
      "All"
    ]
  },
  "0003_Spring": {
    "param": [
      "Select_Param",
      "row_color_0",
      "row_color_1",
      "row_color_2",
      "row_color_3",
      "row_color_4",
      "All"
    ],
    "value": [
      "Select_Param",
      "value_00",
      "value_01",
      "value_02",
      "value_03",
      "value_04",
      "All"
    ]
  },
  "0004_Autumn": {
    "param": [
      "Select_Param",
      "dev_x_0",
      "dev_x_1",
      "dev_x_2",
      "dev_x_3",
      "All"
    ],
    "value": [
      "Select_Param",
      "value_00",
      "value_01",
      "value_02",
      "value_03",
      "All"
    ]
  }
}


function make_option_1(data, select, value, value_1 = null) {
  option = $("<option>")
    .attr({
      text: data,
      value: value,
      value_1: value_1
    }).html(data);
  select.append(option);
}

let temp_json = [];
let json_for_backend = {};

function make_dropdown_1(data) {
  var select_device = document.getElementById("device_n");
  var select_name = document.getElementById("name_n");

  var jqr_select_device = $(select_device);
  var jqr_select_name = $(select_name);

  for (let a in data) {
    make_option_1(a, jqr_select_device, a);
    for (let b = 0; b < Object.keys(data[a]["param"]).length; b++) {
      make_option_1(data[a]["param"][b], jqr_select_name, a, b);
    }
  }
  $("#device_n").trigger('change') //call first select
}

$("#device_n").change(function() {
  var id = $(this).val();
  let init_name_n_val = $("#name_n option:selected").val();
  $("#name_n option").hide() //hide all options
  $("#name_n option[value='" + id + "']").show(); //show options where value matches
  $("#name_n option[value_1=0][value='" + id + "']").prop('selected', true); //set second value selected
  
  if($(this).val() == "select device"){
  $("#name_n").val("select parameter");
  }
  else{
  $("#name_n").trigger('change'); //call second select
  }
});

$("#name_n").change(function() {
  var values = $("#device_n").val();
  let name_n_option_text = $( "#name_n option:selected").text();
  let name_n_option_index = data[values]["param"].indexOf(name_n_option_text);
  let updated_value = data[values]["value"][name_n_option_index];
  let value_key = "value";
  if (updated_value != "Select_Param") {
    create_json_for_backend(values, value_key, updated_value);
}
});

make_dropdown_1(data);


function create_json_for_backend(main_key,value_key, updated_value){
if (!json_for_backend.hasOwnProperty(main_key))
{
    var value_temp_json = {};
    var value_temp_list = [];
    value_temp_list.push(updated_value);
    value_temp_json[String(value_key)] = value_temp_list;
    json_for_backend[String(main_key)] = value_temp_json;
}
else{
    if(!json_for_backend[main_key][value_key].includes(updated_value)){
    json_for_backend[main_key][value_key].push(updated_value);
  }
}
}

let input = $("<input>")
                    .attr("id", "id_for_input")
                    .attr('type', 'number')
                    .attr('name', 'value')
                    .attr('placeholder','query')
                    .addClass("all_input_class");
          
submit_button = $("<button>")
                    .addClass("class_for_submit")
                    .text('Submit')
                    .click(function() {
                            var inputVal = document.getElementById("id_for_input").value;
                        json_for_backend["Query"] = inputVal;
                        console.log(json_for_backend);
                    });

let get_div = document.getElementsByClassName("container");
let get_div_jquery = $(get_div);
get_div_jquery.append(input, submit_button);
<!DOCTYPE html>
<html lang="en">

<head>
  <title>JSON Form</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>

<body id="body" style="margin-top: 0">
  <div class="container" style="margin-top:100px;">
      <label for="combination">make combination:</label>
      <select name="device_n" id="device_n" init_text = "ini">
        <option selected="selected" class="grey_color" >select device</option>
      </select>
      <select name="name_n" id="name_n" init_text = "ini">
        <option selected="selected" class="grey_color" >select parameter</option>
      </select>
  </div> 
  
</body>
</html>

So, finally I can again repeat that I need an idea to design a system where user can click on any option and it will toggle the color of that option(also any appearance of tick mark also acceptable).

I found the combination I have given in the upper part is not that much understandable so I am adding here few lines

I am trying to clarify more. Considergreencolor astick mark(just for clarifying). User can select multiple option then tick markwill appear in those selected options. If user again click on any seletced option where alreadytick markis appeared then thattick markwill be gone. Just now, replacetick markwith thegreen colorand disappearance oftick markwithwhite color. That means user can see which options are selected by seeing the tick markor acolor. Eg: 0001_Summerselected and then selectedrow_heat_0and ,row_heat_0androw_heat_2that means user will see these 3 options hasgreencolor. Then if user again click onrow_heat_1then this option will be white and user will seerow_heat_0androw_heat_2aregreennow. user will now choose0002_Winter and select some options there(eg: row_cloud_0, row_cloud_1, row_cloud_4) and all those 3 options will be green. if Now user again choose 0001_Summerthe optionsrow_heat_0androw_heat_2are still green. That means a feeling of tracking user can have.

user10634362
  • 549
  • 5
  • 21
  • Hi, you said `it's corresponding second select row_heat_0 will be green..` but if user select that again it will remove that green colour , but what will happen if user load option again by changing option from first option so `row_heat_0` will be in green color or not ? – Swati May 19 '21 at 06:44
  • if I don't misunderstand your comment then answer is `will be in green color`. I want to see in `gren color` any option which I have selected. And then I can decide whether I will deselect it or not and color changing will make it simpler visually. I have modified the `STEPS` in question. Hopefully it will be more clear now. – user10634362 May 19 '21 at 07:47
  • :P No , what i mean to say is if user unselect `row_heat_0`(change color to white) then when this options will get loaded again `row_heat_0` will be green color or white ? because first option is default selected (set to green). – Swati May 19 '21 at 08:34
  • Then it should be `white`. Actually that's why I have written `I don't know how to disable this. if can disable it also a good leap`. I am thinking on it by adding extra `key` is JSON but which is obviously ot an `optimum solution`. – user10634362 May 19 '21 at 09:01
  • @Swati I have modified the JSON architecture. Now by default any `row_*` will not be selected. – user10634362 May 19 '21 at 11:31

1 Answers1

1

You can get option which is selected by user using (this).find("option:selected") and then use addClass & removeClass classes from your select-box options to change color .

Demo Code :

data = {
  "0001_Summer": {
    "param": [
      "Select_Param",
      "row_heat_0",
      "row_heat_1",
      "row_heat_2",
      "row_heat_3",
      "row_heat_4",
      "row_heat_5",
      "row_heat_6",
      "All"
    ],
    "value": [
      "Select_Param",
      "value_00",
      "value_01",
      "value_02",
      "value_03",
      "value_04",
      "value_05",
      "value_06",
      "All"
    ]
  },
  "0002_Winter": {
    "param": [
      "Select_Param",
      "row_cloud_0",
      "row_cloud_1",
      "row_cloud_2",
      "row_cloud_3",
      "row_cloud_4",
      "row_cloud_5",
      "row_cloud_6",
      "row_cloud_7",
      "row_cloud_8",
      "All"
    ],
    "value": [
      "Select_Param",
      "value_00",
      "value_01",
      "value_02",
      "value_03",
      "value_04",
      "value_05",
      "value_06",
      "value_07",
      "value_08",
      "All"
    ]
  },
  "0003_Spring": {
    "param": [
      "Select_Param",
      "row_color_0",
      "row_color_1",
      "row_color_2",
      "row_color_3",
      "row_color_4",
      "All"
    ],
    "value": [
      "Select_Param",
      "value_00",
      "value_01",
      "value_02",
      "value_03",
      "value_04",
      "All"
    ]
  },
  "0004_Autumn": {
    "param": [
      "Select_Param",
      "dev_x_0",
      "dev_x_1",
      "dev_x_2",
      "dev_x_3",
      "All"
    ],
    "value": [
      "Select_Param",
      "value_00",
      "value_01",
      "value_02",
      "value_03",
      "All"
    ]
  }
}


function make_option_1(data, select, value, value_1 = null) {
  option = $("<option>")
    .attr({
      text: data,
      value: value,
      value_1: value_1
    }).html(data);
  select.append(option);
}

let temp_json = [];
let json_for_backend = {};

function make_dropdown_1(data) {
  var select_device = document.getElementById("device_n");
  var select_name = document.getElementById("name_n");

  var jqr_select_device = $(select_device);
  var jqr_select_name = $(select_name);

  for (let a in data) {
    make_option_1(a, jqr_select_device, a);
    for (let b = 0; b < Object.keys(data[a]["param"]).length; b++) {
      make_option_1(data[a]["param"][b], jqr_select_name, a, b);
    }
  }
  $("#device_n").trigger('change') //call first select
}

$("#device_n").change(function() {
  var id = $(this).val();
  let init_name_n_val = $("#name_n option:selected").val();

  $("#name_n option").hide() //hide all options
  $("#name_n option[value='" + id + "']").show();
  $("#name_n option[value_1=0][value='" + id + "']").prop('selected', true);
  if ($(this).val() == "select device") {
    $("#name_n").val("select parameter");
  } else {
    $("#device_n option").removeClass("selected") //remove from others
    $(this).find("option:selected").addClass("selected") //add selected 
    $("#name_n").trigger('change'); //call second select

  }
});

$("#name_n").change(function() {
  var values = $("#device_n").val();
  let name_n_option_text = $("#name_n option:selected").text();
  //not the first option
  if ($(this).find("option:selected").attr("text") != "Select_Param" && !$(this).find("option:selected").hasClass("selected")) {
    $(this).find("option:selected").addClass("selected") //add selected
  } else {
    $(this).find("option:selected").removeClass("selected") //remove 
  }

  //let ..
  //create_json_for_backend(values, value_key, updated_value);
});

make_dropdown_1(data);
.selected {
  background-color: green;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<select name="device_n" id="device_n" init_text="ini">
  <option selected="selected" class="grey_color">select device</option>
</select>
<select name="name_n" id="name_n" init_text="ini">
  <option selected="selected" class="grey_color">select parameter</option>
</select>

Update 1 :

You can use selectpicker plugin which already has this functionality so all code will remain same just some minor changes you need to make in your current code.

Update code :

var data = {
  "0001_Summer": {
    "param": [
      "Select_Param",
      "row_heat_0",
      "row_heat_1",
      "row_heat_2",
      "row_heat_3",
      "row_heat_4",
      "row_heat_5",
      "row_heat_6",
      "All"
    ],
    "value": [
      "Select_Param",
      "value_00",
      "value_01",
      "value_02",
      "value_03",
      "value_04",
      "value_05",
      "value_06",
      "All"
    ]
  },
  "0002_Winter": {
    "param": [
      "Select_Param",
      "row_cloud_0",
      "row_cloud_1",
      "row_cloud_2",
      "row_cloud_3",
      "row_cloud_4",
      "row_cloud_5",
      "row_cloud_6",
      "row_cloud_7",
      "row_cloud_8",
      "All"
    ],
    "value": [
      "Select_Param",
      "value_00",
      "value_01",
      "value_02",
      "value_03",
      "value_04",
      "value_05",
      "value_06",
      "value_07",
      "value_08",
      "All"
    ]
  },
  "0003_Spring": {
    "param": [
      "Select_Param",
      "row_color_0",
      "row_color_1",
      "row_color_2",
      "row_color_3",
      "row_color_4",
      "All"
    ],
    "value": [
      "Select_Param",
      "value_00",
      "value_01",
      "value_02",
      "value_03",
      "value_04",
      "All"
    ]
  },
  "0004_Autumn": {
    "param": [
      "Select_Param",
      "dev_x_0",
      "dev_x_1",
      "dev_x_2",
      "dev_x_3",
      "All"
    ],
    "value": [
      "Select_Param",
      "value_00",
      "value_01",
      "value_02",
      "value_03",
      "All"
    ]
  }
}

function make_option(data, select, value, value_1 = null) {
  option = $("<option>")
    .attr({
      text: data,
      value: value,
      value_1: value_1
    }).html(data);
  select.append(option);
}

let temp_json = []; // no need here, test purpose for future
let json_for_backend = {}; // no need here, test purpose for future
var data_new = {}; // no need here, test purpose for future

function make_dropdown(data) {
  var select_device = document.getElementById("device_n");
  var select_name = document.getElementById("name_n");

  var jqr_select_device = $(select_device);
  var jqr_select_name = $(select_name);

  for (let a in data) {
    make_option(a, jqr_select_device, a);
    for (let b = 0; b < Object.keys(data[a]["param"]).length; b++) {
      make_option(data[a]["param"][b], jqr_select_name, a, b);
    }
  }
  data_new = data;
  $('#device_n').selectpicker('refresh'); // NEWLY ADDED
  $("#device_n").trigger('change') //call first select
}

$("#device_n").change(function() {
  var id = $(this).val();
  console.log("id: ", id);
  let init_name_n_val = $("#name_n option:selected").val();

  $("#name_n option").hide() //hide all options
  $("#name_n option[value='" + id + "']").show();
  if ($(this).val() == "select device") {
    //$("#name_n").val("select parameter");
  } else {
    $('#name_n').selectpicker('refresh'); //call second select

  }
});
//on change of second select-box
$('#name_n').on('changed.bs.select', function(event, clickedIndex, newValue, oldValue) {
  //get value of clicked option
  var selected_text = $("#name_n option").eq(clickedIndex).text();
  console.log("selected_text: ", selected_text);
});

make_dropdown(data);
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/js/bootstrap-select.min.js"></script>


<div class="container" style="margin-top:100px;">
  <label for="combination">make combination:</label>
  <select name="device_n" id="device_n" class="selectpicker">
    <option selected="selected" class="grey_color">select device</option>
  </select>
  <select name="name_n" id="name_n" class="selectpicker" multiple>
  </select>

</div>
Swati
  • 28,069
  • 4
  • 21
  • 41
  • I have tried it but unfortunately no chnage I have seen in the `option / options` while I have seletct and deselect. Also here in the `code snippet` after running no change is showing. – user10634362 May 19 '21 at 12:57
  • When you select any option it will change color to green you will see that when you open select-box again because after change event we are setting color or you need some other behaviour ? – Swati May 19 '21 at 13:05
  • I actually need something different. maybe my description was not helpful. I am trying to clarify more. Consider `green` color as `tick mark`(just for clarifying). User can select multiple option then `tick mark` will appear in those selected options. If user again click on any seletced option where already `tick mark` is appeared then that `tick mark` will be gone. Just now, replace `tick mark` with the `green color` and disappearance of `tick mark` with `white color`. That means user can see which options are selected by seeing the `tick mark` or a `color`. Edited Ques as it is too long – user10634362 May 19 '21 at 13:20
  • Please see the last part of the question, I have edited it to clarify with example. – user10634362 May 19 '21 at 13:23
  • Sorry but , i am able to see option are working fine (changing color) :/ so i am little bit confuse why you are not able to see that . – Swati May 19 '21 at 13:55
  • that means if you select `0001_Summer` and then `row_heat_0`, `row_heat_1` and `All` can you see these three `options` are in `green` color? And if you click on `All` then `green` color appears on `row_heat_0` and `row_heat_1` while all the other options in dropdown is `white`(while whole dropdown is visible then it can you see?)? I cannot see anything in my `browser` or in the `code snippet` like that. using `Firefox`. – user10634362 May 19 '21 at 14:13
  • Check in chrome and see onces. – Swati May 19 '21 at 14:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232591/discussion-between-swati-and-user10634362). – Swati May 19 '21 at 14:19