Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show div base on onchange from dropdown option, help

I try to show some information inside of <div> as following:

    <div id="show_details" style="'display:block;' : 'display:none;'"> SHOW me
    </div>

by choose from drop down option e.g.

         <?php if ($books == 'art') { ?>
          <option value="art" selected="selected" id="art_indicator" onchange="(this.selected) ? $('#show_details').css('display','block') : $('#show_details').css('display','none');">Art</option>
          <?php } else { ?>
          <option value="art" id="art_indicator" onchange="(this.selected) ? $('#show_details').css('display','block') : $('#show_details').css('display','none');">Art</option>
          <?php } ?>

and the full code as following,

    <tr>
      <td>Book Option</td>
      <td>
      <select name="books">
        <?php  foreach ($others as $other) { ?>
          <?php if ($other == $other['other']) { ?>
          <option value="<?php echo $other['other']; ?>" selected="selected"><?php echo $other['title']; ?></option>
          <?php } else { ?>
          <option value="<?php echo $other['other']; ?>"><?php echo $other['title']; ?></option>
          <?php } ?>
          <?php } ?>

          <?php if ($books == 'art') { ?>
          <option value="art" selected="selected" id="art_indicator" onchange="(this.selected) ? $('#show_details').css('display','block') : $('#show_details').css('display','none');">Art</option>
          <?php } else { ?>
          <option value="art" id="art_indicator" onchange="(this.selected) ? $('#show_details').css('display','block') : $('#show_details').css('display','none');">Art</option>
          <?php } ?>
        </select></td>
    </tr>
    <div id="show_details" style="'display:block;' : 'display:none;'"> SHOW me
    </div>

Is there some way to fix this?

like image 229
omc11 Avatar asked Oct 24 '22 19:10

omc11


1 Answers

<script type="text/javascript">

function showDiv()
{
  // hide any showing
  $(".details_div").each(function(){
      $(this).css('display','none');
  });

  // our target
  var target = "#details_" + $("#test_select").val();                                                                                                                                

  // does it exist?
  if( $(target).length > 0 )
  {
    // show it
    $(target).css('display','block');
  }
}

$(document).ready(function(){

  // bind it
  $("#test_select").change(function(){
    showDiv();
  });

  // run it automagically
  showDiv();
});


</script>
<style>
.details_div
{
  display:none;
}
</style>
{/literal}

<select id="test_select">
  <option value="">- Select - </option>
  <option value="book" selected="selected">Books</option>
  <option value="movie">Movie</option>
</select>

<div id="details_book" class="details_div">BOOK DETAILS</div>
<div id="details_movie" class="details_div">MOVIE DETAILS</div>
like image 91
Josh Avatar answered Oct 27 '22 10:10

Josh