0

I have a code like this:

var formFocus = formFocus || {
    spanElement: ".focusOnLoad form input[name="FirstName"]";
    init: function () {
       $(document).ready(function() {
          //come other computation
          $(formFocus.spanElement).focus();  
        });
    },
};

I am running into issues with this line: spanElement: ".focusOnLoad form input[name="FirstName"]";

I get an error at name="FirstName"

I tried escaping " with spanElement: ".focusOnLoad form input[name=\"FirstName\"]";Then I get Unexpected token ; error.

I can get the code working if I just use .focusOnLoad form input[name="FirstName"] as selector. But I need to use a variable since ist a shared code and some others without the knowledge of this part has to use this variable.

Is there a way to fix this?

5
  • What part needs to be a variable? If you are having trouble nesting quites, just use single quotes: spanElement: ".focusOnLoad form input[name='FirstName']", Commented May 19, 2017 at 20:58
  • 2
    Maybe ; should be changed by , ? You are enumerating members of an object, not statements. Commented May 19, 2017 at 21:00
  • @TodorSimeonov Yes, good catch. And, there shouldn't be a , after the init property. Commented May 19, 2017 at 21:00
  • You can use without any quotes [name=FirstName] Commented May 19, 2017 at 21:02
  • Todor, yeah..thats it! Thanks for the help. Now..I am gonna kick myself to missing that. Commented May 19, 2017 at 21:04

1 Answer 1

1
var formFocus = formFocus || {
spanElement: ".focusOnLoad   form input[name='FirstName']",
 init: function () { 
 $(document).ready(function() { 
 //come other computation 
 $(formFocus.spanElement).focus();
   }); }, };

You have here, you wrote

form input[name="FirstName"]";

Instead of:

form input[name='FirstName']",

The difference is: you terminated the line with semicolon instead of a comma, and also, you used double quote inside another double quote

Hope my answer helps 😉

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

2 Comments

Yes, that is it. That fixed it. Thanks for the help. Now..I am gonna kick myself for missing it.
I am happy it helps 😇

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.