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

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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/>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

Why not check the value on load rather than waiting for a change event to trigger it?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Vincent Puglia
Forum Commoner
Posts: 67
Joined: Thu Sep 04, 2003 4:20 pm
Location: where the World once stood

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

Post by Vincent Puglia »

str = "this isn't BLACK";
alert((str.indexOf("BLACK")!= 0))
Post Reply