0

I found another similar question about this with a solution but it didn't work for me. I am new to jquery so thanks in advance for helping me solve my noobie question.

I am trying to use a variable for the class name. So that I can loop through the right fields based on this variable. My variable returns .reqfields4 but I get this error Error: Syntax error, unrecognized expression: ", if I enter the value manually, it works.

How can I achieve this? Thanks.

var reqsection='\".reqfields4\"';

$(".reqfields4").each ( function() { 
//$(reqsection).each ( function() {               
          if(( $(this).val())==''){
               some code...
               }
          else{
               some code...
              }
            });
1
  • Jquery expects a string object for a class selector, so there is absolutely no reason why you need to escape quotes here. Unless you really wanted to get a class name with a quote inside it (and it won't work, all class names start with dot and not quotemark!).The purpose of putting in quotes inside the selector is if you don't already have a string object! If your string object is in a variable, then you have your string object and that's that!. Quoting = shortcut for new String('whatever') in js, and doesn't mean something else strange or mysterious that requires quote marks for some reason. Commented Sep 10, 2012 at 13:11

4 Answers 4

1

Just change var reqsection='\".reqfields4\"'; to var reqsection='.reqfields4'; - you don't need the inner string quotes.

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

Comments

1

You don't need quotes inside the value of the variable:

var reqsection = '.reqfields4';
$(reqsection).each( ... );

If you have a class name — just a class name — you can add the .:

var reqsection = 'reqfields4';

$('.' + reqsection).each( ... )

Comments

0

You can try like follows,

var reqsection=".reqfields4";

$(reqsection).each ( function() { 

              if(( $(this).val())==''){
                   some code...
                   }
              else{
                   some code...
                  }
                });

Comments

0

If you say var reqsection= '\".reqfields4\"'; then $(reqsection).each( function() {});

Consider it like like you told jQuery this:

$(' ".reqfields4" ').each( function() {}); 

In other words you said: [ Please select now ".reqfields4" including the quotemarks ]. All class selectors must start with dot(.), but you just started your jQuery selector with a quotemark (") so of course it won't work at all (it won't try to select any classes, of course), and probably would throw an error from jQuery - I don't think ANY jQuery selector string can have quotemark(") as the actual first character of the selector!.

If you do this instead:

var reqsection= ".reqfields4";

$(".reqfields4").each( function() {});
$(reqsection).each( function() {}); 

The above two lines are equivalent.

Comments

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.