Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to detect if a particular button or link was clicked with Javascript?

Posted on 2011-09-15
7
229 Views
Last Modified: 2012-08-14
Hi Experts,

I have a coldfusion 9 form that has some ajax processing going on. I need to identify if the user has actually saved a change made on the page or clicked away from the page without saving a value. So I need to perform some action if the user clicks on a couple of links that take them away from the page without saving. How do I do this with jquery or javascript?

0
Comment
Question by:roger_v
  • 4
  • 3
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36543741
http://jsfiddle.net/V4WEN/1/

$("a").click(function() {
    alert("You're going to : " + $(this).attr("href") ); 
})

Open in new window



<a href="http://www.google.com">Google</a>
<a href="http://www.yahoo.com">Yahoo</a>

Open in new window

0
 
LVL 1

Author Comment

by:roger_v
ID: 36543874
@leakim:

I'm a lil confused. So I have a bunch of <a href>s that the user can click to move away from the page. So my function would be like so:

<script language="javascript">
      $(document).ready(function(){
            $('.moveAwayLnk').click(function(){
            $('#textboxthatwaschanged').function();{
            $('#textboxthatwaschanged').value = $('#hiddenNameVal').value;
            };
            });
      });

</script>

As you can see in the script, I have a class of links called moveAwayLnk, and if any of those links are clicked, that means that the user is navigating away from the page and therefore perform a specific function. Does that look right? Also, do I need a specific jquery plugin to perform this or just the regular jquery minified file?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36543922
a better syntax :
$(document).ready(function(){
            $('.moveAwayLnk').click(function(){
                   $('#textboxthatwaschanged').value = $('#hiddenNameVal').value;
            });
});

Open in new window

or :
$(document).ready(function(){
            $('.moveAwayLnk').click(function(){
                   doSomething();
            });
});

function doSomething() {
                   $('#textboxthatwaschanged').value = $('#hiddenNameVal').value;
}

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 82

Expert Comment

by:leakim971
ID: 36543931
ooops...

$(document).ready(function(){
            $('.moveAwayLnk').click(function(){
                   $('#textboxthatwaschanged').val( $('#hiddenNameVal').val() );
            });
});

Open in new window

or :
$(document).ready(function(){
            $('.moveAwayLnk').click(function(){
                   doSomething();
            });
});

function doSomething() {
                   $('#textboxthatwaschanged').val( $('#hiddenNameVal').val() );
}

Open in new window

0
 
LVL 1

Author Comment

by:roger_v
ID: 36544589
@Leakim:

Just one question: How do I do an ajax call with jquery? Currently I use cfajaxproxy and get a proxy js class for the cf component. Then I use an instance of this class in js and call the cfc function like so:

var changeName = function(tv,te){
             var objAppInfo = new ObjApp();
               objAppInfo.setCallbackHandler(updateInfo);
            objAppInfo.setErrorHandler(myErrorHandler2);
      
            objAppInfo.funcUpdateName(tv,te); //this is the remote function in the cfc that I call
}

//my callback function
var updateInfo = function(res){
      if(res==1){
            //location.reload(true);
            //alert('capturing json data structure..');
            return true;
      }
      else{alert('Error Occurred in updating Name!');}
      return false;
}
var myErrorHandler2 = function(statusCode, statusMsg){
      alert('Status: ' + statusCode + ', ' + statusMsg);
      return false;
}

To do the same thing with jquery, how do I modify this?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36544666
not sure about the right syntax here (you should open a new question) but there's multiple way :

$.get("yoururl", {"param1name":param1value, "param2name":param2value}, callbackfunctionname);
or :
$.post("yoururl", {"param1name":param1value, "param2name":param2value}, callbackfunctionname);
or :
$.getJSON("yoururl", {"param1name":param1value, "param2name":param2value}, callbackfunctionname);
or :
$.ajax({ "url":"yoururl", "data":{"param1name":param1value, "param2name":param2value}, "success":callbackfunctionname, "error":callbackerrorfunction });
0
 
LVL 1

Author Closing Comment

by:roger_v
ID: 36544820
thanks much. Will open another question on how to do this in jquery and ajax.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question