I have the following HTML code:
<td align="center" class="shuttleControl">
<img src="/i/mydb/icons/shuttle_reload.png" onclick="g_Shuttlep_v61.reset();" alt="Reset" title="Reset"/>
<img src="/i/mydb/icons/shuttle_last.png" onclick="g_Shuttlep_v61.move_all();" alt="Move All" title="Move All"/>
<img src="/i/mydb/icons/shuttle_right.png" onclick="g_Shuttlep_v61.move();" alt="Move" title="Move" />
<img src="/i/mydb/icons/shuttle_left.png" onclick="g_Shuttlep_v61.remove();" alt="Remove" title="Remove" />
<img src="/i/mydb/icons/shuttle_first.png" onclick="g_Shuttlep_v61.remove_all();" alt="Remove All" title="Remove All" />
Based on the above code, using jQuery, how can I replace all the above onclick calls to instead use my JavaScript function get_Count().
So the result I am after is:
<td align="center" class="shuttleControl">
<img src="/i/mydb/icons/shuttle_reload.png" onclick="get_Count();" alt="Reset" title="Reset"/>
<img src="/i/mydb/icons/shuttle_last.png" onclick=" get_Count();" alt="Move All" title="Move All"/>
<img src="/i/mydb/icons/shuttle_right.png" onclick=" get_Count();" alt="Move" title="Move" />
<img src="/i/mydb/icons/shuttle_left.png" onclick=" get_Count();" alt="Remove" title="Remove" />
<img src="/i/mydb/icons/shuttle_first.png" onclick=" get_Count();" alt="Remove All" title="Remove All" />
</td>
2 Answers 2
I'd say something like:
$('img').unbind('click').click(get_Count);
EDIT:
Well, I tried that, and it doesn't work. What does work is .attr('onclick', ''). So, do this:
$('.shuttleControl img').attr('onclick', '').click(get_Count);
answered Dec 21, 2010 at 23:10
Spiny Norman
8,3271 gold badge35 silver badges57 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
tonyf
@Spiny, I have other img tags throughout my code so how can I pin point to ensure that I am only referencing the shuttleControl class?
user113716
.unbind() is for jQuery handlers.Spiny Norman
@tonsils I edited my post, this should work for you. @patrick Yeah, I just found out about that :)
tonyf
@Spiny - I think this has worked - still testing it. Just another query, how would I target just one line only, say the shuttle_last.png line and only change the onclick call for this line alone? Thanks.
Spiny Norman
@tonsils Well, since the
imgs don't have an id, you'd have to use one of the other selectors (api.jquery.com/category/selectors), for example: $('.shuttleControl img[src="/i/mydb/icons/shuttle_last.png"]') (or: $('.shuttleControl img[src$="shuttle_last.png"]')) |
If you're not needing those functions in g_Shuttlep_v61 at all, you could just overwrite them.
g_Shuttlep_v61.reset =
g_Shuttlep_v61.move_all =
g_Shuttlep_v61.move =
g_Shuttlep_v61.remove =
g_Shuttlep_v61.remove_all = get_Count;
Then there's no need to mess with the inline attributes.
answered Dec 21, 2010 at 23:21
user113716
323k64 gold badges454 silver badges441 bronze badges
2 Comments
tonyf
thanks. where exactly in the code would I set these calls to now use my get_Count call?
user113716
@tonsils: I assume
g_Shuttlep_v61 is a global variable, so just place it in the same scope as your get_Count function.lang-js