views:

2969

answers:

5

Dear all

I am using the JQuery and Javascript.I have onclick event ,that calls the javascript function. How to extract the arguments of function_A and function_B in JQuery And Is it Any Best way to do It? I need to Do ajax Call after javascript Click.Is any performance based issue
while using jQuery

My Code

<html>
            <head>
              <script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
              <script>
               $(document).ready(function(){
                      //here need to extract the Javascript function arguments
                        var id1=from_javascript_function_A
                        var id2=from_javascript_function_B

                        var link1=from_javascript_function_A
                        var link2=from_javascript_function_B
                      //     


              </script>

            </head>
            <body>
               <a href='#' onclick=javascript:function_A('first',1)>link1</a>
               <a href='#' onclick=javascript:function_B('second',2)>link2</a>

            </body>
            </html>
A: 

It seems like you are doing it wrong, though I'm not sure, as it's unclear what you are trying to do. What are you trying to do? Please edit your question and add more information!

If you really really want to extract the parameters from the function calls, you need to do a bunch of manual parsing, eg.

$("a").each(function(){
  var onclicktext = $(this).attr("onclick");
  if(onclicktext.indexOf("function_A") > -1){
    // Do more parsing here
  }
  if(onclicktext.indexOf("function_B") > -1){
    // Do more parsing here
  }  
});
svinto
A: 

Function arguments are meant to be visible within that function only. Typically they are variables, and their value will only have meaning on the moment the function is invoked (in your example, when the corresponding link is clicked). Within $(document).ready, their value is not relevant.

In case the arguments are not variables, they are known and you wouldn't be asking this question anyway.

tehvan
A: 

I suppose you can't edit the html code? If this is true, then all you can do is some ugly hacks:

$("a").each(function(){
  fnStr = ($(this).attr('onclick')).toString();
  re = /\"[^\"]+\"/
  alert(re.exec(fnStr));
  re = /\'[^\"]+\'/
  alert(re.exec(fnStr));
});

The above code uses regular expressions to get the parameters between quotes. Try it yourself, different browsers may have different behavior.

kgiannakakis
+2  A: 

What about something like this?

<script>
     $(document).ready(function(){
      $("a.a").click(function(e){
       DoWork('a',1,function_a);
      });
      $("a.b").click(function(e){
       DoWork('b',2,function_b);
      });
     });

     function DoWork(s,i,f){
      f(s,i);
      alert("after click:" + s + " " + i);
     }

     function function_a(s,i){
      alert("a:" + s + " " + i);
     }
     function function_b(s,i){
      alert("b:" + s + " " + i);
     }

    </script>

</head>
<body>

    <a href="#" class="a">a</a>
    <a href="#" class="b">b</a>
</body>
Mark
A: 

hello all, i have same kind of problem as mentioned above i have a link and i want that onclick event of that link calls a javascript function. using javascript it is easy you can write

<a href="#" onclick="processlink(<?php echo $echo rowcount; ?>);"> Edit<a>

you see here that argument of that function is coming from a php variabl. if i want to do a similar thing in jquery can i do it? if yes how? problems become even worse when my page is loaded from some other page through ajax call and ur id's are created dynamically. thanks in advance regards adeel

`

adeel