Link to home
Start Free TrialLog in
Avatar of Justin
JustinFlag for United States of America

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.

$(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);
        });
    });

Open in new window


thank you
Avatar of Michael Vasilevsky
Michael Vasilevsky
Flag of United States of America image

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.

var capDate

Open in new window

should be
var capDate;

Open in new window


If this is a straight copy and paste, that could be your issue.
Avatar of Justin

ASKER

I am calling this function with an button onclick

 <button type="button" id="SelectType" class="mybtn" onclick="getCaption">Set  Type</button>

Open in new window


 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;

Open in new window


 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>

Open in new window

Michael's response should be your solution.  It was looking for a vairable, not the function to call.
Avatar of Justin

ASKER

Thank you for the reply, I updated my button to:

<button type="button" id="SelectType" class="mybtn" onclick="getCaption()">Set Type</button>

Open in new window


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
Avatar of Michael Vasilevsky
Michael Vasilevsky
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Instead of adding getCaption() as a handler on the <button>, you can try this:
$(document).ready(function() {
  $('#SelectType').click(function () {
    /* rest of code here */
  });
});

Open in new window

Then your <button> would just be:
<button type="button" id="SelectType" class="mybtn">Set Type</button>

Open in new window

Avatar of Justin

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
Avatar of Justin

ASKER

Thank you very much for the assistance.