I am trying to load youtube videos using JS function. The intention is to have the user simply copy and paste the youtube video URL into an input field and then have my JS code takes care of the loading part. Unfortunately I am not sure why js code is not loading anyvideo? JS FIDDLE
JS:
<script>
$(document).ready(function(){
var last_cnad_text_1 = '';
var options_cnad_text_1 = {
embedMethod:'fill',
maxWidth:320,
maxHeight: 320
};
function loadVideo()
{
val = $('#cnad_text_1').val();
if ( val != '' && val != last_cnad_text_1 )
{
last_cnad_text_1 = val;
$("#embed_cnad_text_1").oembed(val,options_cnad_text_1);
}
}
$(function(){
$('#cnad_text_1').keydown(loadVideo());
$('#cnad_text_1').click(loadVideo());
$('#cnad_text_1').change(loadVideo());
});
});
</script>
html:
<html>
<div class="col1">Insert a youtube video Link</div>
<div class="col2">
<input id="cnad_text_1" type="text" name="cnad[text_1]">
<div id="embed_cnad_text_1">
<iframe></iframe>
</div>
</div>
</html>
$('#cnad_text_1').keydown(loadVideo());is wrong. You are calling the function immediately instead of assigning it as a callback for the event. Remove the()and it should work:Actually you can condense it even more: