Page 1 of 1

How do I set a "hide/show" to hide without 'onchange'?

Posted: Tue Nov 12, 2013 6:14 am
by simonmlewis
This is the part of my code that I need altering some how.

This works for our Sell page, as they choose a type, and based on that selection, a box appears with further options.
However, on the Edit page, if they have pre-chosen the type that would have shown the additional box, I want that box to show straight away. but if the Type is NOT the option to make the box show, then it shouldn't.

Code: Select all

  

<style>
#send_to_one{display:none;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  
    $("#send_to_one").hide();
  
    $("input:radio[name='guntype']").change(function(){  

            if(this.value == 'Special Version' && this.checked){
              $("#send_to_one").show();
            }else{
              $("#send_to_one").hide();
            }

    });

});
</script>

<input type='radio' id='send_poll1' name='guntype' value='Normal Version' ";
  if ($row->stype == "Normal Version") { echo " checked";}
  echo " /><label for='send_poll1'>Normal Version</label><br/>     
  <input type='radio' id='send_poll2' name='guntype' value='Special Version'' ";
  if ($row->stype == "Special Version") { echo " checked";}
  echo " /><label for='send_poll2'>Special Version</label><br/>

Re: How do I set a "hide/show" to hide without 'onchange'?

Posted: Tue Nov 12, 2013 6:29 am
by Celauran
Why not check the value on load rather than waiting for a change event to trigger it?

Re: How do I set a "hide/show" to hide without 'onchange'?

Posted: Tue Nov 12, 2013 6:32 am
by simonmlewis
Yes I can check the value of it - no problem, but I don't know how to alter this code to then make the DIV "show".

Re: How do I set a "hide/show" to hide without 'onchange'?

Posted: Tue Nov 12, 2013 7:00 am
by simonmlewis
Bingo - I put the <script> into the PHP echo, and queried the stype result. If it was of the option to Hide, then it showed that first hide() script, if it was the option to show it, then it first set it to show it.

and both options to show and hide still work.

Perfecto!

Re: How do I set a "hide/show" to hide without 'onchange'?

Posted: Tue Nov 12, 2013 7:32 am
by simonmlewis

Code: Select all

<script>
$(document).ready(function(){
  
    $("#send_to_one").hide();
  
    $("input:radio[name='stype']").change(function(){  
            if(this.value == 'Black' && this.checked){
              $("#send_to_one").show();
            }else{
              $("#send_to_one").hide();
            }

    });

});
</script>
I need to modify this slightly now, as I need one so that if they choose an option that contains the word "black", then it shows a DIV.

I don't know how to alter:
if(this.value == 'Black', so that it's more like: if(this.value LIKE '%black%'...

Any ideas?

Re: How do I set a "hide/show" to hide without 'onchange'?

Posted: Mon Dec 02, 2013 5:22 pm
by Vincent Puglia
str = "this isn't BLACK";
alert((str.indexOf("BLACK")!= 0))