0

I have a code like this:

<label>Brand</label></br>
<select name="brand" id="brand" onChange="changecat(this.value);">
    <option value="" selected>Select Brand</option>
    <option value="A">AMD</option>
    <option value="B">Intel</option>
</select></br></br>

<label>Socket</label></br>
<select name="category" id="category" onchange="showProcessor()">
    <option value="" disabled selected>Select Socket</option>
</select>
var brandByCategory = {
  A: ["AM4", "AM3", "AM2"],
  B: ["LGA 1151", "LGA 1151v2", "LGA 1150"]
}

function changecat(value) {
  if (value.length == 0)
    document.getElementById("category").innerHTML = "<option>Select Socket</option>";
  else {
    var catOptions = "";
    for (categoryId in brandByCategory[value]) {
      catOptions += "<option>" + brandByCategory[value][categoryId] + "</option>";
    }
    document.getElementById("category").innerHTML = catOptions;
  }
}

If the brand option is selected, then it will populate the socket drop-down, and it works, but the socket drop-down option doesn't have a value, i have to give a value to it, because i wanna fetch a data from the database which needs the value of the brand. How to add value in the second drop-down?

2
  • 1
    show what you have tried so far? Commented Oct 23, 2019 at 6:21
  • i tried to add key pair values, add string in the between <option> but it doesnt work @AmanjotKaur Commented Oct 23, 2019 at 6:26

3 Answers 3

1
var brandByCategory = {
    A: ["AM4", "AM3", "AM2"],
    B: ["LGA 1151", "LGA 1151v2", "LGA 1150"]
}

function changecat(value) {
    if (value.length == 0) {
        document.getElementById("category").innerHTML = "<option>Select Socket</option>";
    } else {
        var catOptions = "";
        for(categoryId in brandByCategory[value]){
            catOptions += `<option value="${brandByCategory[value][categoryId]}">${brandByCategory[value][categoryId]}</option>`;
                }
            console.log(catOptions)
            document.getElementById("category").innerHTML = catOptions;
        }
    }
changecat('A')
Sign up to request clarification or add additional context in comments.

2 Comments

still, it doesnt have a value in it
there was a small mistake I have corrected it it's working now
1

If you want to add option based on your given array. Then you can follow the below approach.

var brandByCategory = {
    A: ["AM4", "AM3", "AM2"],
    B: ["LGA 1151", "LGA 1151v2", "LGA 1150"]
}

function changecat() {
    let selectedVal = document.getElementById("brand").value;
    var catOptions = "";
    
    for (let i=0; i<brandByCategory[selectedVal].length; i++) {
        let value = brandByCategory[selectedVal][i]; 
        catOptions += "<option value="+value+">" + value + "</option>";

    }
    document.getElementById("category").innerHTML = catOptions;
}
<label>Brand</label></br>
   <select name="brand" id="brand" onChange="changecat();">
      <option value="" selected>Select Brand</option>
      <option value="A">AMD</option>
      <option value="B">Intel</option>
   </select> </br> </br>

<label>Socket</label></br>
   <select name="category" id="category" onchange="showProcessor()">
      <option value="" disabled selected>Select Socket</option>
   </select>

Don't hesitate to let me know if have any confusion/query.

3 Comments

it doesnt work, the select is showing the A and B not the value in it
did you try to run with code snippet? sorry I didn't get your point, would you please explain little bit?
still it doesnt work, i have found the solution, but thankss
0

var brandByCategory = {
  A: ["AM4", "AM3", "AM2"],
  B: ["LGA 1151", "LGA 1151v2", "LGA 1150"]
}

function changecat(value) {
   var sel =  document.getElementById("category");
   sel.innerHTML = '';
   var option = document.createElement("option");
   option.text = "select cat";
   sel.add(option);
   for (categoryId in brandByCategory[value]) {
      option = document.createElement("option");
      option.text = brandByCategory[value][categoryId];
      sel.add(option);
  }

}

// A $( document ).ready() block.
$( document ).ready(function() {
    console.log( "ready!" );
    changecat("A");
    
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

<button  onclick="changecat('A')" type="button"> chnage cat A </button>

<button onclick="changecat('B')">chnage cat B </button>

<div>

<select id="category"/>
</div>

Comments

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.