0

I am new to jquery. I wanted to generated a set of image button so i have done like this

  <script type="text/javascript">
$(document).ready(function(){
     $.support.cors = true;  
    if ($('#ulTask li').length == 0)
    {
        $.ajax({
            type: "POST",
            url: "http://diana/TaskManagerWebService/TaskManagerService.asmx/SelectAllTasks",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            error: OnError
        });
        function OnSuccess(TaskData){        
            var TaskDataObj = jQuery.parseJSON(TaskData.d);                
            $.each(TaskDataObj, function (i, v) {
               var TaskTypeUrl = getTaskTypeImageURL(TaskDataObj[i].TaskType), TaskId = parseInt(TaskDataObj[i].TaskId);
              $("#ulTask").append('<li id="liTask'+TaskDataObj[i].TaskId+'"><div id="divTask'+TaskDataObj[i].TaskId+'"><img id="imgCollapse'+TaskDataObj[i].TaskId+'" alt="' + TaskDataObj[i].TaskId + '" src="Images/story_collapsed1.png" onclick="javascript:FillTaskDetails('+TaskDataObj[i].TaskId+');"/>' + TaskDataObj[i].TaskName + '</div></li>');
            });
        }
        function OnError(request, status, error){                
            alert(request.statusText);
        }
    }
});
</script>

for the image which i binded in li tag have onclick event.

onclick="javascript:FillTaskDetails('+TaskDataObj[i].TaskId+');"

I wanted to assign that click event in query formate like

$("#imageId").click(function(){
});

how to i get this "imageId", Actually this imageid is combination of text and dynamic value

<img id="imgCollapse'+TaskDataObj[i].TaskId+'".......

Thanks in advance

2 Answers 2

1

Create the image those images with an unique class name

<img class="imgCollapse" id="imgCollapse'+TaskDataObj[i].TaskId+'".......

Then bind a live click event for that img class

$(".imgCollapse").live("click",function(event){
    event.preventDefault();
    taskID = $(this).attr('id').replace('imgCollapse','');
    //console.log(taskID);
    FillTaskDetails(taskID);
 });
Sign up to request clarification or add additional context in comments.

1 Comment

ID of an element should be unique for a web page. If you have multiple elements with ID, jQuery Id selector only select only first element, not all elements.
0

try .live()

$("#imageId").live("click",function(){
    // Code is here ...
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.