How to use javascript in posts?


I am trying to grab youtube title using oembed
This is my code with some errors like index.js: Unexpected token (3:12) in codepen. Please correct it.

		  function getyoutubetitle(id) {
		  	var json = '' + id + '&format=json';


um … how about:


this is bad practice however.
You should be doing something like

<span class="youtube" id="xh6mpAOD9ho"></span>
    $('').each(function() {
      var id = $(this).attr('id');
      var json = '' + id + '&format=json';

note: this code is completely untested, not sure whether it works :slight_smile:

#3' + id + '&amp;format=json this is a url and I have doubt that I can simply collect the value in a variable since it is a remote url?


For some reason javascript is not working in posts. Is it default behavior?


I can see in your link you not using any js
as mentioned above:
wrap your code in script tags:


Already tried, please check now <script>getyoutubetitle("iClTTtecJhs");</script>. It loads with some problem…