0

I have a Jquery function that helps with validation over 1 object. I need to expand it so that the function will run over 3 different objects. I am trying to define a function that takes a parameter(whichquote) to insert the appropriate object in the function. Here is my code. What I am doing wrong? I assume I do not have the selector correct as the code works if I put it in.

Original Function that works:

  var depends = function() {
      var selectorD = $("input[name^='lead[quote_diamonds_attributes]'], select[name^='lead[quote_diamonds_attributes]']");
      var vals = '';
      selectorD.not(':eq(0)').each(function () {
          vals += $(this).val();
      });

      return vals.length > 0;
  };

Function I am trying to create that allows me to use it on other objects. This currently does not work.

 var depends = function(whichquote) {
      var selectorD = $("input[name^='lead[+ whichquote +]'], select[name^='lead[+ whichquote +]']");**
      var vals = '';
      selectorD.not(':eq(0)').each(function () {
          vals += $(this).val();
      });

      return vals.length > 0;
  };

I think the problem is with my concating in the var selectorD but cannot seem to get the syntax correct.

1
  • I think you're just missing the quotation marks, try this: var selectorD = $("input[name^='lead["+ whichquote +"]'], select[name^='lead["+ whichquote +"]']"); Commented Jul 22, 2015 at 19:04

1 Answer 1

1

Your selector isn't actually inputting whichquote because the string concatenation is incorrect.

Try

var selectorD = $("input[name^='lead[" + whichquote + "]'], select[name^='lead[" + whichquote +"]']");

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

3 Comments

I tried that with no success. It seems like it should. How would I call the function: depends("quote_diamonds_attributes") or depends(quote_diamonds_attributes)? I have tried both.
With quotes I believe. try to log out selectorD after your assignement.
Yes, it is with quotes. I adjusted some other code and now it seems to be working. Not sure why. I appreciate your help.

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.