1

How to execute jquery using eval function

function hello(){
    console.log("hello");
}

$(function() {
    hello();
   
})(); 

I have above code as String value and passing to java script eval function. It's giving TypeError: $(...) is not a function. How to load Jquery dynamically and execute above code to print "hello" using eval/Function or $.globalEval.

1
  • "How to load Jquery dynamically" is a very different question than the one you asked in the title of your post Commented Jun 26, 2020 at 0:25

1 Answer 1

1

It works fine, you need to remove the last () from $(function() {}).

does this help ?

eval(`
  function hello(){
    console.log("hello");
  }

  $(function() {
    hello();
   
  });

`)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.