简体   繁体   中英

jQuery append after the button

Following were my HTML:

<div class="row attachments-container">
    <div class="row">
        <div class="col-4 row-space-2 h5 invisible" id="js-first-photo-text">
            Your first photo appears in search results!
        </div>
    </div>
    <% if !@product.new_record? %>
        <% @product.product_attachments.each do |attachment| %>
            <div class="col-xs-6 col-md-4 form-group grayscale">
                <img src="<%= attachment.attachment.small.url %>" class="upload-photo-image">
                <input type="hidden" name="product_attachment[id][]" value="<%= attachment.id %>">
                <button class="delete-photo-btn overlay-btn js-delete-photo-btn delete-attachment" data-photo-id="143275119">
                    <i class="fa fa-trash-o"></i>
                </button>
                <button class="cover-photo-btn cover-overlay-btn js-delete-photo-btn cover-attachment" data-photo-id="143275119">
                    <i class="fa fa-picture-o"></i>
                </button>

            </div>
        <% end %>
    <% end %>

    <div class="col-xs-6 col-md-4 form-group">
        <div class="thumbnail panel photo-item empty-photo-frame" name="empty-photo-frame">
            <img src="<%= asset_url('add-image-placeholder.png') %>">
        </div>
    </div>

</div>

When the user decide to change cover following onClick will trigger:

$('.attachments-container').on('click', '.cover-attachment', function (event) {
    $this = $(this);
    var append_html = '<p class="status-notice">Saving as cover....</p>';
    $this.find('.cover-attachment').append_html

    var attachment_id = $(this).parent().find('input[name="product_attachment[id][]"]').val();
    $.ajax({
        type: "POST",
        url: "/product_attachments/" + attachment_id + "/set_cover",
        dataType: "json",
        data: {
            'attachment_id': attachment_id
        },
        complete: function (data) {
            $this.parent().find('.status-notice').text('Saved!').fadeOut('slow');
            console.log(data)
        }
    });
    event.preventDefault();
})

The problem is, the status-notice is not appended after the cover-attachment button.

Problem:

The problem is in the following statement

$this.find('.cover-attachment').append_html

this will return undefined as there is no method/property name append_html on jQuery.


Solution:

Jquery append after the button

To append HTML, you can use append() .

$this.find('.cover-attachment').append(append_html);

To completely replace/overwrite the HTML of element use html() .

$this.find('.cover-attachment').html(append_html);

Jquery append after the button

To add new element after another, use after()

$this.find('.cover-attachment').after(append_html);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM