Click here to Skip to main content
16,016,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every one!
i have a problem with $(jQuery).append() function. here is my function add:
JavaScript
var newElement='<img class="myImg" src="a.jpg" />';
$("#myDiv").append(newElement);

it is called when a button click.
and here is a function handle new element (img).
JavaScript
$(".myImg").click(function(){
    ......
});


the problem is that it doesn't fire.
if there any solution for this one except the below?
JavaScript
var newElement='<img class="myImg"  önclick="doFunction();" src="a.jpg" />';
Posted

$("#booknow").live('click',function() {
//your code
}
 
Share this answer
 
When do you add the $(".myImg").click function? If you add this before you actually add the element then jQuery cannot actually add it to the element because it doesn't exist. What I would do if I were you is add the $(".myImg").click function immediately after you call .append.
 
Share this answer
 
Hi,

I did what you want check this once

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script language="javascript">

   
         function f1() {
             $("#maincontent").append(" <img class="\"myimg\"" src="\"Images/User1/Blue%20hills.jpg\"" />");
             $(".myimg").click(function () {
                 alert("fgdfg");
             });
         }

    </script>
    <style type="text/css">
        .myimg
        {
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="button" onclick="f1()" value="addd Tag" />
        <div id="maincontent" runat="server">
        </div>
    </div>
    </form>
</body>
</html>


Here you've to place that in function

All the Best
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900