The LinkedIn share button can be added to a page with this code:
<script src="//platform.linkedin.com/in.js" type="text/javascript"></script>
<script type="IN/Share" data-counter="top"></script>
I would like to be able to dynamically change the data-counter to be on top of the button.
To do this, I removed the old LinkedIn button
and added a new one with the following code:
addScript = function(counterPosition) {
mainElement = document.getElementById("scripts-go-here");
d = document.createElement("script");
d.type="IN/Share";
d.setAttribute("type", "IN/Share");
d.setAttribute("data-counter", counterPosition);
// Reset the current element, since we only want to change the layout of the button
while(mainElement.firstChild) {
mainElement.removeChild(mainElement.firstChild);
}
mainElement.appendChild(d);
IN.parse();
}
addScript("right");
addScript("top");
The problem though, as you can see in this jsfiddle, is that a few JS errors appear shortly after:
"Uncaught TypeError: Cannot set property 'innerHTML' of null"
Why does this happen and how can I get around this ?
Is this a problem with Linkedin's jsApi ?