Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 410
  • Last Modified:

show table row if select menu equals 1

I have previously used the javascript code below to show / hide a table based a check box:

<script language="javascript">
      $("document").ready(function() {
            if (!$("#phone_support").attr('checked')) $(".captionRow").show();
            $("#phone_support").click(function(){
                  if ($("#phone_support").attr('checked')) $(".captionRow").hide();
                  else $(".captionRow").show();
                  })
            });
</script>

How do I change it so the captionRow class only shows if the value of the below select menu is 1?

<select name="ink_brand_id" id="ink_brand_id">
        <?php
do {  
?>
        <option value="<?php echo $row_rs_ink_brand['id']?>"><?php echo $row_rs_ink_brand['ink_brand']?></option>
        <?php
} while ($row_rs_ink_brand = mysql_fetch_assoc($rs_ink_brand));
  $rows = mysql_num_rows($rs_ink_brand);
  if($rows > 0) {
      mysql_data_seek($rs_ink_brand, 0);
        $row_rs_ink_brand = mysql_fetch_assoc($rs_ink_brand);
  }
?>
      </select>
0
petewinter
Asked:
petewinter
  • 2
  • 2
1 Solution
 
hieloCommented:
try:
if( $('#ink_brand_id option:selected').attr('value')==1 )$(".captionRow").show();
0
 
petewinterAuthor Commented:
Thanks, but it's not working for me. Can you please check:

<script language="javascript">
      $("document").ready(function() {
                  if( $('#ink_brand_id option:selected').attr('value')==1 )$(".captionRow").show();
            $('#ink_brand_id option:selected').click(function(){
                  if( $('#ink_brand_id option:selected').attr('value')!=1 )$(".captionRow").hide();
                  else $(".captionRow").show();
                  })
            });
</script>
0
 
hieloCommented:
my guess you want to show/hide upon change. If so, try:
<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:red; }
  .captionRow{display:none;}
  </style>
  <script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
  <select name="ink_brand_id" id="ink_brand_id">
    <option value="0">Flowers</option>
    <option value="1" selected="selected">Shrubs</option>
    <option value="2">Trees</option>
    <option value="3">Bushes</option>
    <option value="4">Grass</option>
    <option value="5">Dirt</option>
  </select>
  <div></div>
<script language="javascript">
      $("document").ready(function() {
	 	$('#ink_brand_id').bind('change',function(){
			if( this.selectedIndex > -1  && this.options[this.selectedIndex].value==1)
				$(".captionRow").show();
			else
				$(".captionRow").hide();
		});
		$('#ink_brand_id').change();
      });
</script>
<div class="captionRow">Hi</div>
</body>
</html>

Open in new window

0
 
petewinterAuthor Commented:
Perfect. Many thanks for your help!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now