Jump to content

Calling javascript function inside jquery?


w3schoon

Recommended Posts

Nice be with you everyone! My problem is how to call javascript function inside jquery?

<script type="text/javascript" src="javascripts/jquery-1.9.1.min.js"></script> <script>	  function li(){   var A=$(this).text();$(.text(A.substr(0,19)); $("#job").removeAttr("name value");			  $("#position").attr({"name":"jobApplied", "value":A }); $(.css({"color":"#000", "background":"#fff" }); $(C).css({"color":"#a9a9a9", "background":"#d9d9d9"}).text(D); $("#job").css({"background":"#d9d9d9" }); }		  $(document).ready(function(){$("#ul1 li ul a").on("click",function(){ var B="#cut1", C="#cut2", D="Medical positions";  /* I want to call function li() here but it is not working */ });}); $(document).ready(function(){$("#ul2 li ul a").on("click",function(){ var B="#cut2", C="#cut1", D="Technical positions";  /* I want to call function li() here but it is not working */ });}); </script>

Thank you.

Edited by w3schoon
Link to comment
Share on other sites

Sorry for my bad code (above), unfortunately I missed many things. 1st. Through searching I found out that variables with value can be use as agruments, http://www.w3schools.com/js/js_functions.asp 2nd. I noticed that "this" in the var A=$(this).text() has no value inside function li() 3rd. Inorder to have value, "this" should be inside of $("#ul1 li ul a").on("click",function(){ $(this).text(); } 4th. Finally it works! After I passed "this" argument in the li(this, #cut1" "#cut2" "Medical positions"); here is my code and it works!

<script type="text/javascript" src="javascripts/jquery-1.9.1.min.js"></script> <script>           function li(X, D, C, D){               var A=$(X).text();$(.text(A.substr(0,19)); $("#job").removeAttr("name value"); $("#position").attr({"name":"jobApplied", "value":A }); $(.css({"color":"#000", "background":"#fff" }); $(C).css({"color":"#a9a9a9", "background":"#d9d9d9"}).text(D); $("#job").css({"background":"#d9d9d9" }); }           $(document).ready(function(){$("#ul1 li ul a").on("click",function(){ li(this, "#cut1", "#cut2", "Medical positions"); });});           $(document).ready(function(){$("#ul2 li ul a").on("click",function(){ li(this, "#cut2", "#cut1", "Technical positions"); });});           </script>

Link to comment
Share on other sites

this will always have a value, it's the scope that your code is running in. It will default to the window object unless otherwise specified. You can force a function to run in a particular scope by using the Function.call method, for example. https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/call

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...