0

I have two categories in my database table.

Women Fashion and Men fashion

Under Men Fashion, I have Men Shoes

Under Women fashion, I have Female bag

I want to a javascript I will use in HTML Form select so that when one selects Women Fashion, he/she will see list of items under women fashion like women bag in the second form or when one select Man Fashion, he/she will see list of items under Men fashion like Men shoe in the second form as well.

sample

<form class="form">
  <div class="form-group">
    <select id="box1" name="num">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>

    <select id="box2" name="letters">
      <option value="a">A</option>
      <option value="b">B</option>
    </select>
  </div>
</form>

then I have this

$("#box1").on('change', function () {
  var value = $(this).val();

  if (value === "1" || value === "2") {
      $("#box2").show();
  } else {
      $("#box2").hide();
  }
});

but don't know how to implement it using the database value

Please help

1
  • check I just updated it Commented May 28, 2020 at 2:58

1 Answer 1

1

APPLY DRY PHILOSOPHY FOR THIS CODE

It's just a test but is it that behavior you're looking for?

HTML CODE

    <form class="form">
  <div class="form-group">
    <select id="collections" name="collection">
      <option>Select a value</option>
      <option value="man">Man</option>
      <option value="woman">Woman</option>
    </select>

    <select id="box2" name="collections-items">
    </select>
  </div>
</form>

JS CODE

let chooseCollections = document.querySelector('#collections')
let selectItems = document.querySelector('#box2')

let manCollection = {
  categories: ["Shoes", "Shirt"]
}

let womanCollection = {
  categories: ["Dress", "Shirt"]
}

chooseCollections.addEventListener('change', (e) => {
  selectItems.querySelectorAll('*').forEach(el => el.remove());
  selectItems.style.display = "none"

  if(e.target.value === "man") {
    selectItems.style.display = "block"
    for(let i = 0; i < manCollection.categories.length; i++) {
      var option = document.createElement('option')
      var value = manCollection.categories[i]
      option.value = value
      option.innerHTML = value
      selectItems.appendChild(option)
    }
  }

  if(e.target.value === "woman") {
    selectItems.style.display = "block"
    for(let i = 0; i < womanCollection.categories.length; i++) {
      var option = document.createElement('option')
      var value = womanCollection.categories[i]
      option.value = value
      option.innerHTML = value
      selectItems.appendChild(option)
    }
  }
})

CSS

#box2 {
  display: none;
}

I've represent manCollection and womanCollection like the data you have in your database. This code is not optimized and can be more efficient but you can start with that.

https://jsfiddle.net/0aek49fr/1/

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

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.