Reputation: 895
i need to execute posImgCar()
function after after this code
$('details_img').click(function(i){
...
$(this).closest(".page").find(".car_detail").parent().attr('href', $(this).attr('src'));
...
});
so I had this:
$('details_img').click(function(i){
...
$(this).closest(".page").find(".car_detail").parent().attr('href', $(this).attr('src'));
/*--->*/ posImgCar();
...
});
posImgCar();
But it just call the posImgCar()
when I click twice in the same image.
How should I proceed to accomplish it?
Upvotes: 0
Views: 203
Reputation: 220136
I think you're looking for jQuery's .one()
:
$('.details_img').one('click', function(i){
...
$(this).closest(".page").find(".car_detail").parent().attr('href', $(this).attr('src'));
...
posImgCar();
});
From the docs:
This method is identical to
.bind()
, except that the handler is unbound after its first invocation.
Upvotes: 2
Reputation: 26633
In your code example, you were binding a click event handler to $('details_img')
, which means you were binding it to a custom element, e.g., as if your HTML contained <details_img></details_img>
. I assume that details_img is actually a class name or and id.
In your code example, you were also calling postImgCar()
after attaching a click event handler to details_img, presumably when the page loads. If that isn't what you meant to do, then of course take out that extra call.
If you want postImgCar()
to execute each time the user clicks details_img, and after the href attribute is set, do this, if details_img is a class name:
$('.details_img').click(function(i){
// ...
$(this).closest(".page").find(".car_detail").parent().attr('href', $(this).attr('src'));
posImgCar();
// ...
});
Or this, if details_img is an id:
$('#details_img').click(function(i){
// ...
$(this).closest(".page").find(".car_detail").parent().attr('href', $(this).attr('src'));
posImgCar();
// ...
});
Finally, if you are using jQuery 1.6 or later, consider using the .prop()
method, instead of .attr()
. See the jQuery API documentation for .prop() for an explanation.
Upvotes: 2