0

Iam having the following table structure.

HTML:  
<table>
 <tr>
  <td>
   <img id='imgScheduled' onclick='start();'/>
  </td>
  <td>
   <div class='divStatus'>Scheduled</div>
  </td>
 </tr>
</table>
..Same table structure repeated

The above table is defined dynamically where in the img ids are also generated dynamically. I would like to change the divStatus value on the click of img which has some functionality i.e status needs to be changed to "inprogress' using jquery just like $("#imgid").closest something, am not sure.

1
  • Do the img elements have a predictable pattern? Commented Apr 12, 2014 at 13:19

2 Answers 2

3

How about this?

$('body').on('click','td > img',function(){
   $(this).closest('tr').find('.divStatus').html('in progress..');
});
Sign up to request clarification or add additional context in comments.

9 Comments

you can test it in fiddle
You're aware the img and span are in different parent elements, right?
I think closest('tr') is cleaner than parent().parent() in fact. Good job !
@flo you when you use parent its fine when someone else uses its not good approach LoL
@EhsanSajjad, the mistake in your answer wasn't with parent.parent It was with live()
|
1

Bind a click on your image, and find the .divStatus, then change its content !

Remember .text() will espace your argument, instead of .html() that will render DOM elements !

http://jsfiddle.net/XL5Ew/

$(function () {
    $(document).on('click', '#imgScheduled', function (e) {
        $(this).parent().parent().find('.divStatus').text('in progress...');
        // Or with elements
        $(this).parent().parent().find('.divStatus').html($('<span>').text('in progress...'));
    });
});

2 Comments

Not for long I think :)
You're aware the img and span are in different parent elements, right?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.