Reputation: 1196
Somewhere in my code I have the following lines, which I can't change:
<div class="list-clickable" onclick="javascript:update_left_frame('1', $(this));">
<div class="list-clickable" onclick="javascript:update_left_frame('2', $(this));">
<div class="list-clickable" onclick="javascript:update_left_frame('3', $(this));">
// etc...
A few lines of code later, I need to call that same function, so that it updates the left frame. This is generated via PHP by passing a variable number as first param:
<script>
// foo = ??
update_left_frame('3', foo);
</script>
I need to get the reference of the block with the same number, 3 in this example. How do I set up the 'foo' var so that it takes the corresponding block reference? I though about getting:
$('.list-clickable').attr('onclick')
But I can't get it to work.
Thanks in advance!
Upvotes: 0
Views: 1615
Reputation: 1196
OK, I found a solution that works well. I let PHP generate this:
$(document).ready(function() {
var foo;
$('.list-clickable').each(function() {
var attr = $(this).attr('onclick');
if (attr.indexOf('frame(3') !== -1) {
foo = $(this);
}
});
update_left_frame('MY_CODE', foo);
});
Upvotes: 0
Reputation: 48003
It should be
$('.list-clickable').attr('onclick');
If you can give the number do something like this
update_left_frame('3',
$('div').filter(function() {
return this.attr('onclick').match(/update_left_frame\(\'3\'/); //same number as first argument
})
);
Upvotes: 1
Reputation: 6416
Try this:
$('.list-clickable').on('click', function(){
update_left_frame(($(this).index() + 1), $(this));
});
Upvotes: 1
Reputation: 6764
Try setting an id to each of the div elements...
<div id="list-clickable1" class="list-clickable" onclick="javascript:update_left_frame('1', $(this));">
<div id="list-clickable2" class="list-clickable" onclick="javascript:update_left_frame('2', $(this));">
<div id="list-clickable3" class="list-clickable" onclick="javascript:update_left_frame('3', $(this));">
// etc...
Then to get a reference to the div, use $('.list-clickable3') like the example below:
<script>
var ctrl = $('.list-clickable3');
aggiorna_left_frame('3', ctrl);
</script>
Upvotes: 0