0

I have this javascript code

$('select[name="fselect"]').on('change', function() {
  if ($(this).val() == "add") {
    $('.add').show();
  } else {
    $('.blank').show();
    $('.add').hide();
  }
});

$('select[name="selection"]').on('change', function() {

  if ($(this).val() == "project") {
    $('.project').show();
    $('.gallery').hide();
    $('.event').hide();
    $('.vikinglaw').hide();
    $('.btn-project').show();
  } else if ($(this).val() == "galleries") {
    $('.gallery').show();
    $('.project').hide();
    $('.event').hide();
    $('.vikinglaw').hide();
    $('.btn-gallery').show();
  } else if ($(this).val() == "event") {
    $('.event').show();
    $('.gallery').hide();
    $('.project').hide();
    $('.vikinglaw').hide();
    $('.btn-event').show();
  } else if ($(this).val() == "vikinglaw") {
    $('.vikinglaw').show();
    $('.event').hide();
    $('.gallery').hide();
    $('.project').hide();
    $('.btn-vikinglaw').show();
  } else {
    $('.blank').show();
    $('.project').hide();
    $('.gallery').hide();
    $('.event').hide();
    $('.vikinglaw').hide();
    $('.btn-vikinglaw').hide();
    $('.btn-event').hide();
    $('.btn-gallery').hide();
    $('.btn-project').hide();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
  <div class="col-md-12">
    <label for="sel1">What action do you want?</label>
    <select class="form-control" id="fselect" name="fselect">
      <option name="nocontent" id="nocontent" value="nocontent">Please select action</option>
      <option name="add" id="add" value="add">ADD</option>
      <option name="edup" id="edup" value="edup">EDIT/UPDATE</option>
      <option name="delete" id="delete" value="delete">DELETE</option>
    </select>
  </div>
</div>

<div class="add">
  <div class="row">
    <div class="col-md-12">
      <label for="sel1">What page do you want your article to be posted</label>
      <select class="form-control" id="selection" name="selection">
        <option name="nocontent" id="nocontent" value="nocontent">Please select page</option>
        <option name="project" id="project" value="project">Project</option>
        <option name="gallery" id="gallery" value="galleries">Gallery</option>
        <option name="event" id="event" value="event">Event</option>
        <option name="vikinglaw" id="vikinglaw" value="vikinglaw">Viking Law</option>
      </select>
    </div>
  </div>
</div>

What I am trying to do here is that I want to hide first the

$('select[name="selection"]').on('change',function(){

so that if i click

$('select[name="fselect"]').on('change',function(){
if($(this).val() == "add"){
    $('.add').show();
}

the $('select[name="selection"]').on('change',function(){ will show.

Sorry if you don't understand very well english is not my native . I've been doing this for hours but i can't figure out how i can do it. Actually it's already working but i first need to click the add button before it works.

Here's an image so that you can fully understand me

enter image description here

As you can see on the image

On the "What action do you want" on the first load the "What page do you want your article to be posted" must be hide . But the problem is that it's not hiding

9
  • Please make the snippet do what you expect. It is hard to understand what you mean by hide $('select[name="selection"]').on('change',function(){ - You can assign the handler in the click if you want Commented Jul 12, 2018 at 13:29
  • @mplungjan I edited my question . And added some info Commented Jul 12, 2018 at 13:31
  • And as a start you can use switch instead of if else if Commented Jul 12, 2018 at 13:32
  • @mplungjan sir i dont quite understand sorry Commented Jul 12, 2018 at 13:44
  • Please add the relevant HTML to the snippet I made. We are missing some stuff like btn-... Commented Jul 12, 2018 at 13:49

1 Answer 1

1

Maybe just give initial visibility on page load.

$(document).ready(function() {
    $('.add').hide();
});

After all, when page finish loading there was no change on select field to trigger the hide for add div.

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

2 Comments

Glad you figured out what he meant.
@mplungjan sorry about that

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.