i’m having a bit of a prblem with the jquery click function, below is part of the layout of my code:
<td id="01" class="day-main-diary">
<div title="sdfsdf" class="diary-event ui-corner-all">
<span id="delete"></span>
<div class="diary-event-title ui-corner-all">
sdfsdf
</div>
<div class="diary-event-body ui-corner-all">
<p class="hyphenate">sdfsdf</p>
</div>
</div>
</td>
The title, text in diary-event-title and text in diary-event-body are all dynamically generated and the class ‘day-main-diary’ has a click function bound to it (the actual code in the js file is:
$('.day-main-diary').click(function(){
// Code is here
}
i am looking for a way for a click event to be recognised on the span with the id of ‘delete’,
amongst other things i have tried
$('.day-main-diary #delete').click(function(){
and
$('#delete').click(function(){
however, each time it completely ignores this event and always calls the day-main diary click event.
can anyone help me? i will be eternally greatfull
-Matt
First, remember that there can only be one
#deleteID on the page. If there’s more than one, you’ll have issues, and should make it a class.delete.If the
#deleteelement isn’t present when the page loads (dynamic) you can use.live(), which will take care of handling the event for you, no matter when you add the element.But note that this will still fire the event on the
.day-main-diaryas well.One other option would be to call
.delegate()on the.day-main-diary(as long as it is there when the page loads). This is similar to.live()but is more localized.This also will fire the
clickon the.day-main-diary. Not sure what the expected behavior is.Yet another option would be to test the
e.targetof the click in the.day-main-diaryclick handler. If it is the#deletedo one thing, if not, do another.Otherwise, still assuming that the
#deleteis dynamic, you can bind the.click()when you create it.