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
Justin HullAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael VasilevskySolutions ArchitectCommented:
What do you mean you're getting an undefined error? You mean newCap is being returned as undefined? Or you're getting an error?
0
Jeffrey Dake Senior Director of TechnologyCommented:
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.
0
Justin HullAuthor Commented:
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.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Michael VasilevskySolutions ArchitectCommented:
try:

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

Open in new window

1
Jeffrey Dake Senior Director of TechnologyCommented:
Michael's response should be your solution.  It was looking for a vairable, not the function to call.
0
Justin HullAuthor Commented:
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.
0
Michael VasilevskySolutions ArchitectCommented:
Is there a reason you have the function wrapped in $()? Maybe try:

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mikkel SandbergFront End Developer (Contract)Commented:
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

0
Justin HullAuthor Commented:
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
1
Justin HullAuthor Commented:
Thank you very much for the assistance.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.