0

I'm using append to add an image to a <p id="p10">

$('#p10').append("<img src='plus.png' id='clicker' />");

I want to reference the image I've added via the append with the toggle:

$('#clicker').toggle(function() {
        // toggle code here
}, function() {
    // toggle code here
});

I cannot access the image element via the id to use the toggle. If I comment out the toggle, the image shows up on the page. As soon as I add the toggle code, the image blinks for a second and then disappears.

Dman100
  • 747
  • 2
  • 23
  • 49

2 Answers2

0

That is because the version of .toggle that accepts function arguments that should execute serially has been removed. (deprecated since v1.8, removed since 1.9)

.toggle only shows/hides an element now. So calling the .toggle on the clicker just hides it.

For an alternative implementation see What to use instead of `toggle(...)` in jQuery > 1.8?

Community
  • 1
  • 1
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
0
var imgTag = "<img>";
    imgTag.attr('src','plus.png');
    imgTag.attr('id','clicker');
    $('#p10').append(imgTag);

    $(imgTag).toggle(function() {
            // toggle code here
    }, function() {
        // toggle code here
    });
Jimish Gamit
  • 1,014
  • 5
  • 15