You need to do some debugging to get to an answer because we can't tell from the code where the failure may be taking place. This code is poorly written though. The YouTube embed exemplar may be very old.
Anyway, here's the first mistake I see. I don't know if this is a copy and paste error or if your code actually has 2 script tags. Get rid of the one that includes the type. Type isn't needed anymore.
Second, you're mixing JavaScript with jQuery in a jQuery function. While it's perfectly okay to do this from a programming perspective, it makes code easier to read when you use jQuery syntax in a jQuery file. It's a personal choice though.
This is just my 2-cents about adhering to coding standards. May I offer you a free copy of the "Masterminds Software Development Standards"?
Have you debugged the code and examined the results in your browser's development console?
Using console.log(), look for the answers to these questions:
What are the values of the $use_field and entry_id after they are initialized?
What is the value of div#entryid_div after the function runs?
Does the div have the value and not displaying on the screen?
Is the div visible on the screen? Is the CSS color correct and compliant with Accessibility Guidelines?
Are there other jQuery errors in the console that could be preventing my code from running?
These are observations about the code specifics that could present obstacles for meeting your objectives. I've included recommended improvements, as well.
1 You named both variables using different naming conventions. $use_field is written as a jQuery variable, while entryid_id is written as a JavaScript variable without the $. Again, these are both correct and a matter of personal choice. But for code readability, it's inconsistent. My personal habit is not to use the $ with variables in jQuery code so it's not confused with PHP variables if generating jQuery on the fly in PHP.
2. You removed the code trigger. The change() function is the trigger that made this code work with YouTube. When you remove a code trigger, the function executes in the document ready event. If this is the behavior you're targeting, then removing the trigger is the right thing to do.
3. With jQuery, this code is overly complex and can be reduced by a few lines. You don't need to build a call to a field val() by using variables to copy the field first. I don't know of any recommended reason to do this and I've been writing jQuery for a long time. This is just an unnecessary extra step in my mind.
You can get the var entryid_id in one line of code: var entryid_id = $('#field_vkzob').val();
4. You are missing the quotes when you initialize "$use_field = $(#field_vkzob);", anyway.
$use_field is the copy of #field_vkzob field object. When copying a field object, it should be "var $use_field = $('#field_vkzob');". jQuery won't copy the field object without the quotes inside the parens.
5. Get rid of the getElementById(). Use $('div#entryid_div') to insert the URL into the div. You can find plenty of examples on the web about how to do this in jQuery.
You may have to assign the URL to a variable first before inserting it into the div. It's possible to do this in a single line of code if extend the entryid_id variable declaration to build the URL in a single step. You can then write the variable to the div in a second step. You can potentially reduce this function down to 2-lines of code.
Good luck!
i have tried to figure out how to resolve this error without success, can anyone help?
Uncaught SyntaxError: Private field '#field_vkzob' must be declared in an enclosing class
Please login or Register to submit your answer