Forums

Showing a field when "Ja" is selected

SPABO 28 Jan, 2012
Bob, in v3 I have some forms with JS in it
Basically it does the following

When EG)select_2 == Ja, a field will appear in the form.
On "Nee" the field does not show

So...how to set this up in V4??

This the old code
<?
$doc =& JFactory::getDocument();
$script = "window.addEvent('domready', function() {
  $('select_2').addEvent('change', function(event) {
    var e = new Event(event);
    if (e.target.value == 'Ja') {
      $('Voornaam').getParent().getParent().setStyle('display', 'block');
    } else {
      $('Voornaam').getParent().getParent().setStyle('display', 'none');
    }
  });
});";
$doc->addScriptDeclaration($script);
?>
GreyHead 29 Jan, 2012
Hi SPABO,

Drag a Load JS action itno the OnLoad event, move it up before the Show HTML action and add the script there:
window.addEvent('domready', function() {
  $('select_2').addEvent('change', function(event) {
    var e = new Event(event);
    if (e.target.value == 'Ja') {
      $('Voornaam').getParent().getParent().setStyle('display', 'block');
    } else {
      $('Voornaam').getParent().getParent().setStyle('display', 'none');
    }
  });
});
Check that the input ids match up.

Bob
SPABO 29 Jan, 2012
I did Bob...but than website does not load completely as well
I have this in now (with php-tags)
<? 
window.addEvent('domready', function() {
      $('intro2).addEvent('change', function(event) {
      var e = new Event(event);
      if (e.target.value == 'Ja') {
       $('Intro').getParent().getParent().setStyle('display', 'block');
        } else {
        $('Intro').getParent().getParent().setStyle('display', 'none');
        }
      });
    });
?>


Here the code of the Field, but I recaal "something had to be added?

<div class="ccms_form_element cfdiv_text" id="intro_container_div"><label for="Intro">Naam intr</label><input id="Intro" maxlength="150" size="30" class="" title="" type="text" value="" name="Intro" />
<div class="clear"></div><div id="error-message-Intro"></div></div>
GreyHead 29 Jan, 2012
Hi SPABO,

This is JavaScript and doesn't need the 'php' tags. They may be what is breaking your page.

Bob
SPABO 29 Jan, 2012
I tried it without the tags as weel, same result I'm afraid
GreyHead 30 Jan, 2012
Hi SPABO,

Please post a link to the form so I can take a quick look.

Bob
SPABO 30 Jan, 2012
Bob, Site is not on-line, so I attach the form as such
Hope this will do?
GreyHead 30 Jan, 2012
Hi SPABO,

This line is missing a ' after intro2:
$('intro2).addEvent('change', function(event) {


When that's fixed selecting Nee hides the whole form which probably isn't' what you want.

Bob
SPABO 30 Jan, 2012
I have added the "'" now , website behaves now normal.

Indeed, it's not what I wish

If "Ja" is selected, Field Name "Intro" should appear, if "Nee" has been selected, this field shoold still be hidden
GreyHead 30 Jan, 2012
Hi SPABO,

Then these lines are wrong
$('Intro').getParent().getParent().setStyle('display', 'block');
maybe just one getParent, or better check the id for the Intro code block and use that.

Bob
SPABO 30 Jan, 2012
Hi Bob
A combination...

On the form code ( custommode) this is the field (added class display,none)
<div class="ccms_form_element cfdiv_text" style="display: none" id="intro_container_div">


and using your code
$('Intro').getParent().setStyle('display', 'block');


This was the tric

By the way, the select is set standard on "Nee" , otherwise, it disappears only when Nee is selected
This topic is locked and no more replies can be posted.