How do i append Jquery?

So I have a project, where it will append Jquery to the website. For some odd reason, the append isn’t working, and the jquery script isn’t running.

document.head.append('<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>');

$(document).ready(function(){
  $(this).click(function(){
    alert('hi');
  })
})
1 Like

Hi @DaxCodes thanks for your question. Can you share a link to the Repl please so we can see the error?

I’m super sorry, but I actually went to a different solution. Apologies! :cry:

In the regular thing, it would result in a error:

$ is not defined!
1 Like

I know you’ve solved this, but just looking at the code, you’re appending a string to the head of the document, not a script tag.
You’d need to create a script element, then set its attributes and then append that to the document’s head:

const script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js");
// note that you could use script.type and script.src, but using the setAttribute method is considered better practice
document.head.append(script);
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.