0

I am trying to pass an object to a javascript function which should trigger when a link is clicked. But unfortunately I am not able to get it working.

Here is the code on my jsp file for the same:

<script type="text/javascript">
    $(document).ready(function() {
        function tryToDoItTwice(obj) {
            var check = obj;
            if(check == null) {
                return true;
            }
            else {
                alert("You're not allowed to do this twice!");
                return false;
            }
        }
    });
</script>

<a href="<c:url value="/somepath" />" onclick="javascript: tryToDoItTwice(${foo});">Try to do it twice</a>

Here foo refers to an object of type Foo.

Could someone help me understand what am I doing wrong here?

EDIT: Foo is a java object. and ${foo} is a reference variable referring to Foo java object.

Thanks.

16
  • I would say learn Javascript before JQuery. What is ${foo} never seen such syntax. Commented Jan 22, 2012 at 5:24
  • Also, no need to put javascript inside the onclick attribute. And, give us the error message. Commented Jan 22, 2012 at 5:25
  • I need a clarification.. the function is never being called right? Commented Jan 22, 2012 at 5:25
  • @Itay: Its a reference to an object of type Foo. Its expression language used on jsp pages. I am passing this object from a controller to this jsp page. Commented Jan 22, 2012 at 5:26
  • 1
    $ is not defined is not defined when you don't include jquery.js. Commented Jan 22, 2012 at 6:02

2 Answers 2

3

Your check variable is locally scoped to that function so it will be redeclared each time the function is called (not storing your value the next time it's clicked.

You need to expand the scope of your variable.

 var check = null;
  $(document).ready(function() {
        function tryToDoItTwice(obj) {
            check = obj;
            if(check == null) {
                return true;
            }
            else {
                alert("You're not allowed to do this twice!");
                return false;
            }
        }
});

Also, if foo exists elsewhere, you probably just want to pass it in like this:

<a href="<c:url value="/somepath" />" onclick="javascript: tryToDoItTwice(foo);">Try to do it twice</a>

Sign up to request clarification or add additional context in comments.

1 Comment

Don't go by the name of the function. I think he is getting the number of clicks from Server-side rather than Client side..
1

You can't pass a JAVA object directly to javascript. Either you need to return as JSON ( look up GSON or Jackson for converting object to JSON ) or pass variables( not object ) to the function.

1 Comment

Oops, I guess I should have explicitly mentioned that in the question. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.