0

I have this code that I need to edit so I can use it on multiple chkBox's and txtBox's.
Currently I can only hide one input field with one check box.
I know HTML and CSS but I am not familiar with JS.
I would like to be able to add a number at the end of each ID.
chkBox1, chkBox2, chkBox3... txtBox1, txtBox2, txtBox3...

Do I need to change getElementById to getElementsByTagName()?

JSFIDDLE for some reason it does not work here...?

This is my current code which hide the text field unless the checkbox is checked:

function showHide(){
                var chkBox = document.getElementById("chkBox");
                var txtBox = document.getElementById("txtBox");
 
                if (chkBox.checked){
                    txtBox.style.visibility = "visible";
                } else {
                    txtBox.style.visibility = "hidden";
                }
            }
3
  • Take a look here in this update fiddle jsfiddle.net/DHE67/3 Commented May 22, 2014 at 18:15
  • 2
    Your code will work if you put it in the head instead of onload. JSFiddle with no code changes. Commented May 22, 2014 at 18:17
  • 1
    Are you actually using jquery? If you are not using jquery in this project, then please remove the tag from the question. Commented May 22, 2014 at 18:20

2 Answers 2

1

The reason your code wasn't working is because it was running onLoad. Your DOM and the onclick were created before the load was complete. You could just move your code into your <head></head> tags and it will work as is. See here, all I did was select the "No wrap - in head", no code changes.


You could also continue to have your javascript run onLoad and remove your onclick and add an eventlistener in the javascript like this:

JSFiddle

var txtBox = document.getElementById("txtBox");
document.getElementById("chkBox").addEventListener("click", function() {
    if (this.checked) {
        txtBox.style.visibility = "visible";
    } else {
        txtBox.style.visibility = "hidden";
    }
});

If you have multiple instances of this, I would change your DOM a bit sort of like this:

<form>
    <div class="option">
        <input type="text" name="txtBox1" class="hiddenInput" />
        <br/>
        <input type="checkbox" name="chkBox1" id="chkBox1" class="showHideCheck" />
        <label for="chkBox1">Click me to show the text box</label>
    </div>
    <div class="option">
        <input type="text" name="txtBox2" class="hiddenInput" />
        <br/>
        <input type="checkbox" id="chkBox2" name="chkBox2" class="showHideCheck" />
        <label for="chkBox2">Click me to show the text box</label>
    </div>
</form>

and do your JQuery like this (since you previously tagged jquery):

$(".hiddenInput").hide();
$(".showHideCheck").on("change", function() {
    $this = $(this);
    $input = $this.parent().find(".hiddenInput");
    if($this.is(":checked")) {
        $input.show();
    } else {
        $input.hide();
    }
});

JSFiddle


Or with pure javascript and the similar DOM as above:

var checkBoxes = document.getElementsByClassName("showHideCheck");
for (var i = 0; i < checkBoxes.length; i++) {
    checkBoxes[i].addEventListener('click', function () {
        var txtBox = getAssociatedTextBox(this);
        if (this.checked) {
            txtBox.style.visibility = "visible";
        } else {
            txtBox.style.visibility = "hidden";
        }
    }, false);
}

function getAssociatedTextBox(ele) {
    var childNodes = ele.parentNode.childNodes;
    for (i = 0, j = childNodes.length; i < j; i++) {
        if (childNodes[i].className == "hiddenInput") {
            return childNodes[i];
        }
    }
}

JSFiddle

Sign up to request clarification or add additional context in comments.

3 Comments

I have moved the code into the <head></head> tags, how could I edit the JS I have to work on separate <input type="text"> and <input type="checkbox"> fields? @smerny
@AlenSubasic, you had JQuery tagged earlier. Doing that kind of thing would be much cleaner with JQuery. Do you have it sourced? Also, are you able to change the DOM?
@AlenSubasic, updated to show how I would use jquery.
0

Try this,

Javascript

$(document).ready(function(){
    $("input[type=checkbox]").change(function(){

        var oTxt = $("#txtBox" + $(this).attr("id").replace("chkBox", ""));

        if($(this).is("checked"))
            oTxt.show()
        else
            oTxt.hide();
    });
});

HTML

<input type="checkbox" id="chkBox1"/>
<input type="textbox" id="txtBox1"/>

<input type="checkbox" id="chkBox2"/>
<input type="textbox" id="txtBox2"/>

7 Comments

The OP's code works. Plus why bother with jQuery when it can be done with some simple plain JavaScript?
Then why people uses jQuery? all that work can be done by javascript too. yes functionality is not complex but it good practice to use such framework, easy to understand and implement. Solution looks much clear.
Most people use jQuery for more complex tasks, not because it looks pretty.
I try to avoid concatenation for ids. I would rather wrap the check/textbox in a div or something and use proximity... or use a data field if the stuff can be at random locations.
@j08691 if @AlenSubasic has not mentioned jQuery tag i would not go for jQuery.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.