Search code examples
jquerytap

Jquery mobile tap on element to remove it


I have a function that appends some html on the fly to a container div. I want to be able to tap on this created html and make it go away, but cant seem to get it to work.

So for example if i have this function created from a tap on another element:

function functionName(e){
    $('<div />').attr({id: 'someid '}).text(' Text Text Text').appendTo('.container');
 } 

I want to be able to then remove it like this:

$('someid').tap(function(){
        $(this).remove();
    }

Any idea how to make this work?


Solution

  • You haven't added the # to the selector to add the touch event to the someid element, as well as your ending );...

    $('#someid').tap(function(){ ... });
    

    You might also need to use something like the .live() function to 'on-the-fly' add the tap event handler to any elements that are added dynamically to the page's content. Something like:

    $('#someid').live('tap', function(){ ... });