1

Im using JQuery to insert HTML code into a webpage

<div class="title-OthProb-outer">    
        <input type="button" class="title-add-non-standard-issue" value="Add a Non Standard Problem" />
</div>

Jquery:

var titleString = '<div class="title-OthProb-wrap title-nonStand1"><h3>Non Standard   Problems</h3><!-- redacted for readability! --></div><input type="button" class="title-add-another" value="+" /><br>';

$('div[class^=title-OthProb-wrap]').hide();
$('input[class^=title-add-another]').hide();
$(function() {
    $('.title-add-non-standard-issue').on('click', function() {
    $('input[class^=title-add-non-standard-issue]').hide();
       var that = this;
       var elem = $(that).closest('.title-OthProb-outer').append(titleString);
       var elem = $(that).closest('.title-OthProb-outer').find('.title-OthProb-wrap');
       $(elem).fadeIn(500);
});

This works fine, however I want the facility to clone the html, I had it working when the html was all in the page, i.e. not generated by Jquery, however now clicking the 'title-add-another' button does nothing.

$(function() {
$('.title-add-another').click(function() {
    // Add non-standard problems
    var num     = $('.title-OthProb-wrap').length; // how many "duplicatable"  fields we currently have
    var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added

    // create the new element via clone(), and manipulate it's ID using newNum value
    var newElem = $('.title-nonStand' + num).clone().attr('class', 'title-OthProb-wrap title-nonStand' + newNum);

    // insert the new element after the last "duplicatable" input field
    $('.title-nonStand' + num).after(newElem);         
    });
});

If I enter that into the console then the button works... How does Jquery deal with elements that don't exist until after the page has been loaded?

6
  • 1
    delegation, a wonderful word (world) Commented Jun 21, 2013 at 13:13
  • am I missing something or are you trying to clone a non-existing element? Commented Jun 21, 2013 at 13:14
  • 1
    @scunliffe oh well, looks like you are right Commented Jun 21, 2013 at 13:16
  • Checking the pages event handlers shows that theres nothing set for the add-another button once its added. I'll look into delegation. Commented Jun 21, 2013 at 13:16
  • 1
    So this element $('.title-nonStand' + num) already exists when you try to clone it, right? Commented Jun 21, 2013 at 13:18

1 Answer 1

2

You need to use delegation...

$(function () {
    $(document).on('click','.title-add-another',function () {
        // Add non-standard problems
        var num = $('.title-OthProb-wrap').length; // how many "duplicatable"  fields we currently have
        var newNum = new Number(num + 1); // the numeric ID of the new input field being added

        // create the new element via clone(), and manipulate it's ID using newNum value
        var newElem = $('.title-nonStand' + num).clone().attr('class', 'title-OthProb-wrap title-nonStand' + newNum);

        // insert the new element after the last "duplicatable" input field
        $('.title-nonStand' + num).after(newElem);
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

but you should not use document but instead the closest static container of '.title-add-another' elements, it's better

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.