How to set an element via jQuery/Javascript?

Asked

Viewed 197 times

0

I’m doing an integration with a social plugin and I’m having a problem.

My code:

linkedin : function(self){
  var sett = self.options.buttons.linkedin;
  $(self.element).find('.buttons').append('<div class="button linkedin"><script type="IN/Share" data-url="'+(sett.url !== '' ? sett.url : self.options.url)+'" data-counter="'+sett.counter+'"></script></div>');
  var loading = 0;
  if(typeof window.IN === 'undefined' && loading == 0){
    loading = 1;
    (function() {
      var li = document.createElement('script');li.type = 'text/javascript';li.async = true;
      li.src = '//platform.linkedin.com/in.js'; 
      var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(li, s);
    })();
  }
  else{
    window.IN.init();
  }
},

The code above is responsible for creating something like:

<script src="//platform.linkedin.com/in.js" type="text/javascript"></script>
<script type="IN/Share" data-counter="top"></script>

But I need it to create equal above though, adding lang: pt-BR before the </script> in the script pulled from Linkedin.

Thus:

<script src="//platform.linkedin.com/in.js" type="text/javascript">
      lang: pt_BR
</script>
<script type="IN/Share" data-counter="top"></script>

Would such an adaptation be possible? How?

2 answers

4


Define what is inside as the innerHTML (or textContent) tag:

li.innerHTML = 'lang: pt_BR';
  • Perfect! My widget :D is now in English

  • Okay @ramaral, thanks for the feedback.

1

Not that it’s really necessary but since we were asked to solve the problem with pure Javascript or also with jQuery and I will be right-footed and leave the alternative here too

$( document ).ready( function() {
    
    $( '#div' ).append(

        $( '<script>' ).attr({

          'type':  'text/javascript',
          'src':   '//platform.linkedin.com/in.js',
          'async': 'true'

        }).text( 'lang: pt_BR' )
    )

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div"></div>

Need to inspect the element and look for the DIV #div to see, since it is a tag <script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.