0

Hi guys i am trying to implement jQuery in my website. I have used name property of a control to bind an event like below

 $('[name = "OCCUPATION"]').typeahead({
             source: function (query, result) {
                var name = "test";
                $.ajax({
                    url: "@Url.Action("lookupQuestionOption", "Home")",
                    data: { Key: query, type: name},
                    dataType: "json",
                    type: "GET",
                    success: function (data) {
                        result($.map(data, function (item) {
                            return item;
                        }));
                    }
                });
            }
        });

Now i want this single event handler for multiple controls, like it can be OCCUPTION2 , OCCUPTION3, OCCUPTION4, OCCUPTION5...

So the code may be like

$('[name = "OCCUPATION,OCCUPATION2,OCCUPATION3,OCCUPATION4"]').typeahead({

I have tried the above but it's not working. Please help!

3
  • 3
    You can try this $('[name="OCCUPATION"],[name="OCCUPATION2"]') Commented Jun 12, 2019 at 10:36
  • api.jquery.com/attribute-starts-with-selector Commented Jun 12, 2019 at 10:36
  • Alternatively, if there are a lot of inputs, each with different names, use a class instead and bind it to that. Commented Jun 12, 2019 at 10:36

1 Answer 1

1

You can use selector like

it will select all element whose name starts with OCCUPATION

$('[name ^= "OCCUPATION"]')
Sign up to request clarification or add additional context in comments.

3 Comments

Note: Without the spaces.
it will work check jsfiddle.net/djt9k5w0 [console] it select all elements
It will work, but it is still better practice to write it correct.

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.