0

I'm having an issue where I cannot seem to pass the variable "notes" into the placeholder of a form. More Info Below Code:

   request.done(function(data) {

                   window.notes = data;
                   //document.write(notes);

                });
                request.fail(function() {
                   // document.write("fail");
                                     });

                $(document).ready(function(){
                    $('form').find("textarea").each(function(ev)
                    {
                        if(!$(this).val()) {


                           // document.write(notes); Says undefined?
                            $(this).attr("placeholder",window.notes);
                        }
                    });
                });

The placeholder code works...if I use

  $(this).attr("placeholder","test");

It works without problem as it should, but it will not if I try to use the var notes.

The setting of var notes works as well. If I uncomment the document.write("notes") I get the value I expect.

I have tried:

I originally started with Var notes = data; but moved to window.notes based on a suggestion by another question to make it global. Did not work. I also tried "forcing" the variable to be global by setting outside the function scope empty, but that didn't work either.. Is there something I'm missing?

Thanks!

3
  • its a variable scope issue. Commented Nov 25, 2018 at 2:46
  • I assumed that, but I don't seem to be able to resolve that. Any suggestions? Commented Nov 25, 2018 at 2:52
  • You can't use it because the variable is not as global so you have to pass it into function and get it back Commented Nov 25, 2018 at 2:54

1 Answer 1

1

Sorry I don't have much credit to comment . I just wanted to say that the document.ready function works on load at the starting of the page load while request.done or request.fail works after it so initially on load the window.notes is undefined. And when you use the code document.write(notes) it gets defined and hence no error.

Sign up to request clarification or add additional context in comments.

1 Comment

That was it. Rookie mistake on my part. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.