-3

I got this script which works perfectly fine on fiddle, but not working on my website.

http://jsfiddle.net/P2Ab2/30/

Code on site:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(".hiddenInput").hide();
$(".showHideCheck").on("change", function() {
    $this = $(this);
    $input = $this.parent().find(".hiddenInput");
    if($this.is(":checked")) {
        $input.slideDown();
    } else {
        $input.slideUp();
    }
});
</script>
</head>
<body>
<form>
    <div class="option">
        <input type="checkbox" name="chkBox1" id="chkBox1" class="showHideCheck" />Click me to show the text box
        <br/><div class="hiddenInput"> Enter
        <input type="text" name="txtBox1"/></div>
    </div>
    <div class="option">
                <input type="checkbox" id="chkBox2" name="chkBox2" class="showHideCheck" />Click me to show the text box
        <br/><div class="hiddenInput">Enter
        <input type="text" name="txtBox2"/></div>
    </div>
</form>
</body>
</html>
MohitC
  • 4,541
  • 2
  • 34
  • 55

4 Answers4

0

You are executing

$(".hiddenInput").hide();

on the header, before the element is loaded. Try putting your script after the elements on body:

...
    <div class="option">
            <input type="checkbox" id="chkBox2" name="chkBox2" class="showHideCheck" />Click me to show the text box
    <br/><div class="hiddenInput">Enter
    <input type="text" name="txtBox2"/></div>
</div>
<script type="text/javascript">
    $(".hiddenInput").hide();
    $(".showHideCheck").on("change", function() {
        $this = $(this);
        $input = $this.parent().find(".hiddenInput");
        if($this.is(":checked")) {
            $input.slideDown();
        } else {
            $input.slideUp();
        }
    });
</script>

OR setup a function to execute on load event:

<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    function start() {
        $(".hiddenInput").hide();
        $(".showHideCheck").on("change", function() {
            $this = $(this);
            $input = $this.parent().find(".hiddenInput");
            if($this.is(":checked")) {
                $input.slideDown();
            } else {
                $input.slideUp();
            }
        });
    }
</script>
</head>
<body onload="start()>
    ...
</body>
adrield
  • 629
  • 6
  • 19
0

The problem is that you're trying to use the elements before they are even loaded in the page. So, there are two simpler ways to solve your problem.

First

You can use the jQuery's document ready event:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function() { // encapsulate your code into a function and pass it to jQuery
  $(".hiddenInput").hide();
  $(".showHideCheck").on("change", function() {
    $this = $(this);
    $input = $this.parent().find(".hiddenInput");
    if($this.is(":checked")) {
        $input.slideDown();
    } else {
        $input.slideUp();
    }
  });
});
</script>
</head>
<body>
<form>
    <div class="option">
        <input type="checkbox" name="chkBox1" id="chkBox1" class="showHideCheck" />Click me to show the text box
        <br/><div class="hiddenInput"> Enter
        <input type="text" name="txtBox1"/></div>
    </div>
    <div class="option">
                <input type="checkbox" id="chkBox2" name="chkBox2" class="showHideCheck" />Click me to show the text box
        <br/><div class="hiddenInput">Enter
        <input type="text" name="txtBox2"/></div>
    </div>
</form>
</body>
</html>

Second

You can mantain your code intact, but put the script before the end of the body tag:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
<form>
    <div class="option">
        <input type="checkbox" name="chkBox1" id="chkBox1" class="showHideCheck" />Click me to show the text box
        <br/><div class="hiddenInput"> Enter
        <input type="text" name="txtBox1"/></div>
    </div>
    <div class="option">
                <input type="checkbox" id="chkBox2" name="chkBox2" class="showHideCheck" />Click me to show the text box
        <br/><div class="hiddenInput">Enter
        <input type="text" name="txtBox2"/></div>
    </div>
</form>
<script type="text/javascript">
$(".hiddenInput").hide();
$(".showHideCheck").on("change", function() {
    $this = $(this);
    $input = $this.parent().find(".hiddenInput");
    if($this.is(":checked")) {
        $input.slideDown();
    } else {
        $input.slideUp();
    }
});
</script>
</body>
</html>
Buzinas
  • 11,597
  • 2
  • 36
  • 58
0

Wrap it in a document.ready:

$(document).ready(function(){

    $(".hiddenInput").hide();
        $(".showHideCheck").on("change", function() {
            $this = $(this);
            $input = $this.parent().find(".hiddenInput");
            if($this.is(":checked")) {
               $input.slideDown();
           } else {
              $input.slideUp();
           }
       });
 });
Jasper Giscombe
  • 313
  • 1
  • 5
0

Checkout jQuery JavaScript not working on Wordpress, confused about no-conflict mode syntax if your website happens to be a wordpress site.

If wordpress, try:

  jQuery(document).ready(function( $ ) {

   // $ Works! You can test it with next line if you like
   // console.log($);
   // put code here

   });

if you are not using wordpress, try this:

$( document ).ready(function() {
  // put code here
});
Community
  • 1
  • 1
eternalminerals.com
  • 395
  • 1
  • 4
  • 14