0

I'm making a webpage with a submit form where you can submit multiple time periods. On each period you can add extra comments when you click on a checkbox. This works fine for 1 period but when I add more periods with .clone() in jQuery it alters all checkboxes.

How should I resolve this problem?

3
  • 1
    Without some code and some markup we would only be guessing at a solution to your issue. Can you provide some of that? Maybe a jsfiddle? Commented Apr 29, 2014 at 13:03
  • Show HTML you selecting and jQuery selectors you use. Commented Apr 29, 2014 at 13:05
  • Here you go :) jsfiddle.net/P3URK Commented Apr 29, 2014 at 13:26

1 Answer 1

1

You have some mistakes in javascript codes. First, you have to show/hide textbox only for that section. In order to do this, you can use closest() and find() methods of jQuery. Secondly, when checking checked property, it is better to use $(this) of jQuery because javascript's this is something different there. Finally, when clonning an element you should use true parameter if you want to clone actions of elements, too.

Working codes are:

$( document ).ready(function() {
    $("#tijdstiptoevoegen").on( "click", function(e){
        e.preventDefault();
          $( ".tijdstip" ).clone(true).appendTo( ".tijdstippen" );
    });

    $(".chkUitleg").on("change",function() {
        if($(this).is(":checked")) {
        $(this).closest(".tijdstip").find(".verdere_uitleg").show();
    }
    else
    {
        $(this).closest(".tijdstip").find(".verdere_uitleg").hide();
    }
});
  });
Sign up to request clarification or add additional context in comments.

2 Comments

However when I click on add tijdstip the second time I gives me 2 tijdstippen instead of 1.
If found the solution. var i = 0; var cloneid = $( '#tijdstip'+ i ); var prev = i; var previd = $( '#tijdstip'+ prev ); i = i + 1; $( previd ).clone(true).attr('id', 'tijdstip'+(i) ).find("input:text").val("").end().insertAfter( cloneid );

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.