2

I have a link with id="something":

html

<a id="something" onclick="getReturnValue()"> Something </a>

javascript

function getReturnValue(){
  //some validation
  if(validation fails){
    return false;
  }else{
    return true;
  }
}

I need to get the return value when I use:

$("#something").click();

How can I get the return value for the event ?

Already onclick function is mentioned in the html tag. I wanted to use:

$("#something").click(); 

and from that I wanted to get return value.

As I have to use it in multiple place, I dont want to write getReturnValue() method again and again like:

click(function(){ getReturnValue() })

I need to do this

if($("#something").click() == true){
  form.submit();
}

click() will call getReturnValue() which will do some validation. If the validation fails, it return false and I need not submit the form. If validation is true, I need to submit the form

4
  • I can't seem to figure out why you want to retrieve the value from the event handler. Jun 7, 2012 at 23:30
  • I need to do this if($("#something").click() == true){ form.submit(); } click() will call getReturnValue() which will do some validation. If the validation fails, it return false and I need not submit the form. If validation is true, I need to submit the form
    – Tivakar
    Jun 8, 2012 at 14:16
  • If you want to do some form validation before form submission, you just took the wrong approach. You are complicating your code. See my answer below. Jun 8, 2012 at 15:45
  • @nebulousGirl There are lots of other languages/frameworks that are able to return values from an event. .NET and Wordpress (do_action) are a couple notable examples. It is more applicable with custom events (.trigger('some_event')) than with DOM events. I like to trigger custom events before+after AJAX calls so different components can hook into the event, possibly modify the data, and possibly alter the resulting action. For example, my default response to an ajax call might be to reload the page but in certain situations, I may want to redirect instead.
    – Jeff
    Aug 16, 2017 at 22:15

5 Answers 5

1

To do some form validation, you can do something like that:

$('#myForm').submit(function(e) {
    if () {//Validation rules failed
        e.preventDefault(); //Prevent browsers from submitting
    }
    //Form is valid do nothing and let the form submit
});

Now on your button:

$("#something").click(function() {$('#myForm').submit();//Calls function from above});
1

What you can do is create a jQuery event object and pass that to the click handler.

var e = jQuery.Event('click');
$('#something').click(e)

if (e.validResult) {
    //action
}

You'll have to modify the click handler somewhat to take an event object and set a validResult property if the validation is successful.

function getReturnValue(e) {
    //some validation
    if (valid) {
        e.validResult= true;
    }
}
1

Here is an example on how you can access return data from event handler.

var a;
$(document).on('a', function(e){
    $(document).trigger(a = $.Event('sum', {datas: e.datas}));
});
$(document).on('sum', function(e){
    return e.datas;
});
$(document).trigger($.Event('a', {datas: [1,2,3]}));
console.log(a.result);//[1,2,3]

You change it with your purpose. check here https://api.jquery.com/event.result/ for more details.

0
$("#something").click(function() {
    var x = getReturnValue();
});

this is what you want?

LE: and this means your a changes to

<a id="something"> Something </a>
6
  • No.. Already onclick function is mentioned in the html <a> tag. I wanted to use $("#something").click(); and from that I wanted to get return value from it. Thanks for responding.
    – Tivakar
    Jun 7, 2012 at 22:47
  • @Tivakar so you want to fire the click event from javascript, and get that value Jun 7, 2012 at 22:50
  • yes!!! I have to fire the click() event multiple time, so I don't want to use $("#something").click(function() { var x = getReturnValue(); }); It would be better if get return results from $("#something").click();
    – Tivakar
    Jun 7, 2012 at 22:52
  • The only idea I have is to change the function getReturnValue(){ return false; } into a getReturnValue() { callbackMethod(false) }, and callbackMethod is the method where you need the false value Jun 7, 2012 at 22:55
  • Or save the false value in a global variable, and when you need it, get it from this variable. Jun 7, 2012 at 22:57
0

you're trying to run two different functions on a click event, maybe you need remove the inline code (onclick="func()") and put this in your script

$("#something").click(
  function(){
    var returnValue = getReturnValue();
  }
);

I don't really understand your question though...

3
  • Already onclick function is mentioned in the html <a> tag. I wanted to use $("#something").click(); and from that I wanted to get return value. I dont want to mention getReturnValue() method again in click()..
    – Tivakar
    Jun 7, 2012 at 22:50
  • @Tivakar Yes, you've said that, but what do you mean by "get a return value". A return value from what? What do you want to do with this return value?
    – Bojangles
    Jun 7, 2012 at 23:03
  • return value should be sent from gerReturnValue() method. Based on the return value, I need to submit a different forms.
    – Tivakar
    Jun 7, 2012 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.