GriMel
GriMel

Reputation: 2320

Hide dynamically created button by clicking on it

In my Django project I have a ajax search which returns a list of results along with button "Add"

What I want:

  1. Click "Add"
  2. Result is added to div on page
  3. Button "Add" disappears

Work p. 1-2 but not 3

jquery-ajax

$(document).on('click', '.button-add', ->
    catid = $(this).attr("data-catid")
    title = $(this).attr("data-title")
    url = $(this).attr("data-url")
    $.get('/test/auto_add_page/', {category_id:catid, title:title, url:url}, (data) ->
        $('#pages').html(data)
        $(this).hide()
        ))

Upvotes: 1

Views: 83

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599796

this inside the get now refers to a different object. Save it in a variable in the outer block and refer to that.

$(document).on('click', '.button-add', ->
    button = $(this)
    catid = button.attr("data-catid")
    title = button.attr("data-title")
    url = button.attr("data-url")
    $.get('/test/auto_add_page/', {category_id:catid, title:title, url:url}, (data) ->
        $('#pages').html(data)
        button.hide()
    ))

Upvotes: 1

Related Questions