Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery "after-click" event

I have a list of links that open in an iframe, like:

<ul id="frameTrigger">
    <li><a target="iframe1" href="aaa.html"><img src="aaa.jpg"></a></li>
    <li><a target="iframe1" href="bbb.html"><img src="bbb.jpg"></a></li>
    <li><a target="iframe1" href="ccc.html"><img src="ccc.jpg"></a></li>
</ul>

I need to update another div after the link is clicked. Specifically, I need to call a function that checks the frame's src attribute and update the div. If I do a:

$("#frameTrigger a").click(function() {
    var iframe = $("iframe[name=iframe1]").get(0);
    console.log(iframe.contentWindow.location.href);
    // the problem here is that that the iframe.window.location will
    // change AFTER this function returns
});

I do not get what is expected.

like image 900
Salman A Avatar asked Apr 16 '11 08:04

Salman A


People also ask

How do you call a function after a click?

Method 1: Use addEventListener() Method Get the reference to the button. For example, using getElementById() method. Call addEventListener() function on the button with the “click” action and function passed as arguments.

How do you return a click function?

You have to pass a function to click , not its return value. In the event handler you can call your function and deal with the return value as you see fit: $("some_div"). click(function() { var result = my_function(); // do whatever you want with `result` });

How to stop click using jQuery?

To disable a button with jQuery you need to set the disabled property on the button using the prop method. For example $('. my-button'). prop('disabled', true) .

How to disable click event javascript?

$("#id"). on('click', function(){ //enables click event //do your thing here }); $("#id"). off('click'); //disables click event // If you want to disable a div, use the following code: $("#id"). attr('disabled','disabled');


2 Answers

$("#frameTrigger a").click(function(){
    console.log( $(this).attr("href") );
});

You need the first selector to work on clicks for the links. Also, I think you had the syntax for console.log incorrect (though I have never used it). I changed it so when you click on a link it logs the href attribute of the link. Hope this helps.

like image 37
Andrew Jackman Avatar answered Nov 15 '22 13:11

Andrew Jackman


I'm guessing you get the old value instead of the new one? You may want to wrap your function in a setTimeout of 1ms to allow the update to go through.

like image 167
Brad Avatar answered Nov 15 '22 13:11

Brad