Justin
asked on
js function returning undefined
I am sure this is very simple but im not understanding it currently,
Why am I getting an undefined error from this function? The function works but im not see what is giving me the error.
thank you
Why am I getting an undefined error from this function? The function works but im not see what is giving me the error.
$(function getCaption() {
var today = new Date();
$('#SelectType').click(function () {
var capDate
if ($("#month").val() === '1' && $("#year").val() === '2018') {
capDate = " " + today.getMonth() + "/" + today.getFullYear();
}
else {
capDate = " " + $("#month").val() + "/" + $("#year").val();
}
var newCap = "Viewing " + $("#TypeForm").val() + " for " + capDate;
$('#newCaption').html(newCap);
});
});
thank you
What do you mean you're getting an undefined error? You mean newCap is being returned as undefined? Or you're getting an error?
Since you don't have an error message, I am kind of guessing. If this doesn't work, try putting what the error message is. However I do see you are declaring a variable without a ; at the end.
If this is a straight copy and paste, that could be your issue.
var capDate
should bevar capDate;
If this is a straight copy and paste, that could be your issue.
ASKER
I am calling this function with an button onclick
When i click the button I get the error:
Error: 'getCaption' is undefined
after that everything works as expected. The function builds the string and displays it correctly.
Thank you Jeffery,
I added:
but it did not correct the error.
Thank you both for responding.
<button type="button" id="SelectType" class="mybtn" onclick="getCaption">Set Type</button>
When i click the button I get the error:
Error: 'getCaption' is undefined
after that everything works as expected. The function builds the string and displays it correctly.
Thank you Jeffery,
I added:
var capDate;
but it did not correct the error.
Thank you both for responding.
try:
<button type="button" id="SelectType" class="mybtn" onclick="getCaption()">Set Type</button>
Michael's response should be your solution. It was looking for a vairable, not the function to call.
ASKER
Thank you for the reply, I updated my button to:
but i am still getting the same error when clicking the button.
error: Error: 'getCaption' is undefined
this is really racking my brain, it seems to be a typo type of thing but I am just not seeing it.
<button type="button" id="SelectType" class="mybtn" onclick="getCaption()">Set Type</button>
but i am still getting the same error when clicking the button.
error: Error: 'getCaption' is undefined
this is really racking my brain, it seems to be a typo type of thing but I am just not seeing it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Instead of adding getCaption() as a handler on the <button>, you can try this:
$(document).ready(function() {
$('#SelectType').click(function () {
/* rest of code here */
});
});
Then your <button> would just be:<button type="button" id="SelectType" class="mybtn">Set Type</button>
ASKER
I figured out what my issue was i think, I had my function in a scripts.js file which i included. It seems that the onclick event was checking for the function before the include file was loaded? I pulled the function out of the include and put it at the bottom of the cshtml page to get the error to go away.
I guess i do not fully understand the load sequence of includes vs the checks to see if functions exist.
Thank you all for your help
I guess i do not fully understand the load sequence of includes vs the checks to see if functions exist.
Thank you all for your help
ASKER
Thank you very much for the assistance.