0

I need to add a piece of javascript (Linkedin sharebutton) to a div, but I cant access the DOM directly. I can use Javascript/jquery to alter the DOM though so I was wondering if its possible to append Javascript to a certain div, or even better create a div inside the container and target the script there?

The script:

<script src="//platform.linkedin.com/in.js" type="text/javascript"> lang: en_US</script>

The html:

<div class="left"></div>

2 Answers 2

4

You can use javascript for this.

Example:

var scriptTag= document.createElement( "script" );
scriptTag.type = "text/javascript";
scriptTag.src = "//platform.linkedin.com/in.js";
$(".left").append(script);

Try it

EDIT: To keep it elegant you can use .appendChild, instead of .append

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

Comments

0

Try this code, it creates nested container (as you ask) inside your "left" div and creates script tag inside this container:

$(".left").html(
  $("<div>").html(
    $("<script>").attr({ 
      "type": "text/javascript", 
      "src": "//platform.linkedin.com/in.js"
    })
  )
);

Do not foreget to reference jQuery.

Comments

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.