0
create{
   var view;
   $(".l_item").click(function(){
      view = this.value;
   });
   console.log(this.view);
}

This is my jquery function used to get the selected value of the I_item (array of list).after getting this value i need to use this value outside the function

But i get undefined value..so how to achieve this ? any help will be appreciated,

2 Answers 2

1

Define Your Variable Outside click function then use that variable inside your test() method

$scope.view;
$(".l_item").click(function(){
  $scope.view = this.value;
});

Then you can access the view variable outside the click function but it will work

test(){
  console.log($scope.view);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.
0

You haven't mentioned here the Angular Version which you are using here. So, I am providing you the logic in context to the angular 2. Please follow below steps:-

1) If the jquery is installed then write following piece of code to access jquery inside the component.

declare var $: any;

2) It is always a good practice to write all the jquery in some external JS file, instead of writing at component level. So I am assuming that you already have an external JS file which has a function test() inside it.

3) Now at component level, in order to access that function you need to write the following line of code

declare var test: any;

4) Now in case you want that test() function should return some value, then inside the JS file you need to return that value as follows:-

test(){
return "some value";
}

5) You can get the value from jquery function at component with the following line of code :-

var returnValue=test();

6) Do remember that your external js file should load after the main jquery file. You can add external jquery file to the angular module by declaring it inside angularCLI.json as follows:-

"scripts": [
    "../node_modules/jquery/dist/jquery.js",
    "../src/assets/js/core/testmodule/customtest.js"
  ], 

All the best.

3 Comments

i have only a simple jquery code to use, so i prefer to use that inside the component itself. i install jquery and import it in the component as mentioned above .but still not get it..result is undefined so what i need to change in my code i am using angular5
Try to modify the code as follows. Pass event or e inside the function brackets as parameter and then use that parameter to fetch any value. create{ $(".l_item").click(function(el){ var id = $(el).attr('id'); alert(id); }); }
sorry i m not really understand the thing, i got value inside the jquery click function, it not get outside the function

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.