0

I have a fairly simple script designed to hide/unhide various form fields based on customer type. The code looks as follows:


var customerTypeFields = [];
    customerTypeFields['Individual'] = ['First Name(s)', 'Last Name', 'Date of Birth'];
    customerTypeFields['Limited Company'] = ['Name', 'Company Number'];
    customerTypeFields['Partnership'] = ['Name', 'Company Number'];

    $('#customer-type-selector').chosen().change( function() {
        var visibleFields = customerTypeFields[$(this).children("option[value='" + $(this).attr('value') + "']").text()];
        console.log(visibleFields);
        $.each(visibleFields, function(i, field) {
            $('#customer-features').find('input[data-feature-type=' + field + ']').parent().parent().show();
        });
    });

This appears to work to some extent, but I'm getting a strange error when loading an individual, which is:

Uncaught Error: Syntax error, unrecognized expression: input[data-feature-type=First (s)] 

You will note that First (s) is not in my array, and as such it seems like the word Name is being stripped - any idea why the string would be edited like that?

Thanks!

1
  • 1
    Try including it inside a quotes, $('#customer-features') .find('input[data-feature-type="' + field + '"]') .parent().parent().show(); Commented Sep 26, 2012 at 15:45

1 Answer 1

1

Escape with double quotes:

(...)  .find('input[data-feature-type="' + field + '"]')  (...)

See more on jQuery's Attribute Equals Selector page. It basically defines it as:

jQuery('[attribute="value"]')

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

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.