0

I have read this post: Passing extra parameters to source using Jquery UI autocomplete
I am develping a Web Page in Asp.net C#.

My HTML code:

 <input class="tags" to_search="Birds" />
 <input class="tags" to_search="Animals" />

Javascript:

$(document).ready(function() {
     $(".tags").autocomplete({
          source: "GenericHandler.ashx?name="+$(this).attr("to_search")
    });
});

I want to pass the to_search attribute of the <input> tag to generic handler.
Above code is calling GenericHandler.ashx but gives null value of name.
How can I get name value equals to the to_search attribute of <input> tag?
Please help.

2 Answers 2

2

Assuming that you don't add any .tags during runtime, you can do this.

$('.tags').each(function(i, tag) {
    $(tag).autocomplete({
        source: 'GenericHandler.ashx?name='+ $(tag).attr('to_search')
    });
});

I also suggest you use data attributes in your input tags like this.

<input class="tags" data-search="Birds" />

resulting in the final solution,

$('.tags').each(function(i, tag) {
    $(tag).autocomplete({
        source: 'GenericHandler.ashx?name='+ $(tag).data('search')
    });
});

In the event that you do add a element to your document dynamically you can do this.

// jquery object of the new element created
var element;

element.autocomplete({
    source: 'GenericHandler.ashx?name='+ element.data('search')
});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks @Austin, its working perfect. One quick question: If I add any .tags runtime, do I need to call above function after adding .tags dynamically? will it work fine?
Yes you would need to run that snippet again. But it would be more efficient if you just binded .autocomplete to the element individually after you add it to the document. I've updated my answer to include that scenario
-1

Just Pass like this

$('#txtCropname').autocomplete('Handler/CropSearch.ashx', { extraParams: {ids:'new'} });

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.