1

I have only:

<div>
    <textarea id="names"></textarea>
</div>

I can't modify html. I can use only jQuery. Is possible make something like:

<input type="checkbox" id="yes">
<div style="display:none">
   <textarea id="names"></textarea>
</div>

$("#yes").click(function(){
    $("#names").parent().show();
})

Default if page is loaded and ready should be only:

<input type="checkbox" id="yes">

If i checked this then show me div with textarea.

Is possible make without modify HTML? If yes, how?

FIDDLE: http://jsfiddle.net/2ZMQz/

0

3 Answers 3

3
var checkbox = $('<input type="checkbox">');
var parent   = $('#names').parent().hide().before(checkbox);

checkbox.click(function() {
    parent.toggle();
});

http://jsfiddle.net/2ZMQz/14/

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

4 Comments

You're missing the id in the checkbox
Thanks, but it's not necessary for this example.
You could do var checkbox = $('<div>TEST TEST</div><input type="checkbox">');
note that this would add a clickhandler to the text, too. You should create another Object for that.
1

Try the following

$("#names").parent().hide().before('<input type="checkbox" id="yes" />');

You can improve the checkbox functionality by using toggle instead of show

$("#yes").click(function(){
    $("#names").parent().toggle();
})

As the name suggests it will toggle the visibility, so if you deselect the checkbox it hides the content.

http://jsfiddle.net/2ZMQz/7/

3 Comments

When #yes is checked, your textarea is hidden
+1. Beat me to it with same code, may as well have my fiddle [edited].
@DirkShags Your own text works for me, what's the issue you're experiencing?
0

Yes, you can do this.

$("#names").parent().hide().before('<input type="checkbox" id="yes">');

$("#yes").click(function() {
    if ($(this).is(':checked')) {
        $("#names").parent().show();
    } else {
        $("#names").parent().hide();    
    }});

http://jsfiddle.net/2ZMQz/15/

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.