0

i trying to hide input box for name if checkbox gets checked:

if($(#1step).checked){
     $("input[username="true").hide
}

<input username="true">
<input type="checkbox" id="1step"><label>step1</label>
0

3 Answers 3

4

Some notes:

1) Wrap your value in quotes ' ' or double quotes " "

2) Use $("input[username='true']") instead of $("input[username="true")

3) Use <label> instead of <lavel>

4) Use change() event to keep track when your checkbox has been checked or unchecked

5) Trigger change event manually to make sure that the visibility of your textbox has been set properly on page load

Final code look like:

$(function () {
    $('#1step').change(function () {
        if ($('#1step').is(':checked')) {
            $("input[username='true']").hide();
        } else {
            $("input[username='true']").show();
        }
    }).change();
});

Fiddle Demo

or you can shorten your code to:

$(function () {
    $('#1step').change(function () {
        $("input[username='true']").toggle(this.checked);
    }).change();
});

Fiddle Demo

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

1 Comment

thank you added my fade exampel with yours very much hepl thanks
3

You need to have change event handler listening to the change in the checked state of the checkbox and update the visibility of the textbox accordingly(you can use .toggle() to set it). Also you can manually fire the change() event after the handler is added so that the initial visibility of the text field will be set properly

jQuery(function ($) {
    $('#1step').change(function () {
        $('input[username="true"]').toggle(this.checked)
    }).change();//initialize with correct display state
});

Demo: Fiddle

Comments

3

Something like this:

$('#1step').change(function() {
    $("input[username='true']").toggle(this.checked);
}).change();

Make sure to trigger change event initially in case you have HTML coming with checked="true" by default.

Demo: http://jsfiddle.net/z4LQG/

Comments

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.