views:

90

answers:

3

Apologies for the very basic question - crash course in javascript coming up! But before that, I have a javascript function:

 function toggle(request)

The function simply toggles a div as visible/invisible. I can trigger the behaviour I want from this function using an onclick event as follows:

<input type="radio" name="complete" value="true" id="complete_yes" 
onclick="toggle('true');"/>

This works fine, but after posting a form, under certain conditions I want the radio button to be automatically selected and the javascript function to be automatically set to:

toggle('true')

... thus revealing the div contents. I've got the "checked" part working, but how do I get the javascript function to conditionally be set to true aswell?

<input type="radio" name="complete" value="true" {if $data.complete == true}checked{/if} id="complete_yes" 
onclick="toggle('true');"/>
+1  A: 

What about testing, in your JS toggle function, if the div has to be made visible or not depending on the "checked" status of your input ?

Something a bit like this, I guess :

function toggle()
{
    if (document.getElementById('YOUR_INPUT_ID').checked) {
        document.getElementById('YOUR_DIV_ID').style.display = 'block';
    } else {
        document.getElementById('YOUR_DIV_ID').style.display = 'none';
    }
}

Absolutly not tested, but I think you'll get the idea :-)
(I never know if the input in considered as "checked" before or after the JS function on onclick is called... So you might actually have to invert the 'block' and 'none' settings)

And, if you have several inputs/divs, you can pass their ids as parameters to the toggle function...

That way, you'd never have to worry about any kind of "initialization" for that function.


Note that you could also test if the div is visible, with something like this :

function toggle()
{
    if (document.getElementById('YOUR_DIV_ID').style.display == 'none') {
        document.getElementById('YOUR_DIV_ID').style.display = 'block';
    } else {
        document.getElementById('YOUR_DIV_ID').style.display = 'none';
    }
}

(not tested either)

That way, your function would really toggle the div's displaying, each time it is called.

Pascal MARTIN
Thanks - sounds promising, will test tomorrow!
chriswattsuk
If you are finding that the .checked property may not be altered until after the click event fires - try wrapping the whole if statement in a setTimeout(funciton(){ if....else... }, 100); - So it checks a little after you click for the checked property?
gnarf
I'm not having a huge amount of success with this unfortunately - what should the id be in your first example 'YOUR_INPUT_ID' - is this "complete_yes"? Take a look at my code snippet.
chriswattsuk
A: 

Why not do something like:

{if $data.complete == true}
<script type="text/javascript">toggle('true');</script>
{/if}
Quamis
+2  A: 

Decided to learn jQuery instead! The syntax below achieves the desired result and is very maintainable!

$(document).ready(function () {
 $('#complete_yes').toggle(function () {
 $('#repair_complete').removeClass('hidden');
 }, function() {
 $('#repair_complete').addClass('hidden');
});
});
chriswattsuk