StenW
StenW

Reputation: 2004

Trying to understand Jquery appendTO

I am kind of new to Jquery and am trying to understand appendTo().

The problem is that when the form is submitted nothing is appended to the div. The code script works fine as long as no value is chosen, meaning the alert is triggered.

$('#blankett_form').submit(function() {
    var id = $(this).find('.update:last').val();
    if (id == '') {
        alert('Välj land och region.'); 
    } else {
        var table = '<table class="table table-hover table-bordered"><thead><tr>    <td>blanketter.</td><td>datum tillagt.</td></tr></thead></table>'
        $(table).appendTo('#formsubmit');
    }
});

The table should be appended to the following div.

<div id="#formsubmit"> 

</div>

Upvotes: 0

Views: 54

Answers (1)

wirey00
wirey00

Reputation: 33661

remove the # from the div id <div id="#formsubmit">

<div id="formsubmit"> 

</div>

FIDDLE

or you have to escape the #

$(table).appendTo('#\\#formsubmit');

FIDDLE

As others mentioned if the form is being submitted then the page will probably refresh and you won't see any changes.

You can either return false or use event.PreventDefault inside the handler to stop the default submit action

$('#blankett_form').submit(function(e) { // <-- event argument passed in
     e.preventDefault(); // prevent default action
     // your code here
}

or

$('#blankett_form').submit(function() {      
     // your code here
     return false;  
}

Upvotes: 1

Related Questions