Jump to content
w3schoon

Calling javascript function inside jquery?

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

Share this post


Link to post
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>

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...