0

I have a text field and if user type something in it, I want to show suggestion of names with ajax then user will select any name that will be set in the text field then user can submit the form.....

<form>
    <input type="text" id="name" name="name" value="" />
    <input type="submit" value="Submit" />
</form>

here is the form.... guess after getting the response from ajax I displayed the suggestion in a div then if user select/click any name that name will be in the value of the text field.... but the problem is when I display the name then I can't access it later with jQuery.... any help??

<div class="ajax-response">
    <p>Name 1</p>
    <p>Name 2</p>
    <p>Name 3</p>
    more like this.........
</div>
4
  • How about an autocomplete plugin like jqueryui.com/autocomplete? Commented Jan 29, 2015 at 16:06
  • bro there will be a massive name... I need ajax to get name from database.... Commented Jan 29, 2015 at 16:24
  • @BlackReaper The jQuery UI autocomplete plugin supports using a server-side data source (see "Remote datasource" in the documentation), so you'd still be able to query your database for the list of names. Commented Jan 29, 2015 at 16:32
  • For that I need to load another Library if it is possible with jQuery then I would not add UI Library :) Commented Jan 29, 2015 at 17:23

2 Answers 2

1

You've got a div element with a class of ajax-response that contains a series of p elements that have text which is your names. The easiest solution would be to delegate an event handler to p elements inside div.ajax-response, like so:

$('div.ajax-response').on('click', 'p', function(e) {
    // one of your names was clicked on
    $('#name').val($(this).text()); // set the value of your #name input to be the text of the clicked element
});

Note that this requires the same .ajax-response element to always exist on your page, and the code would need to be called at a point when that element exists in the DOM (e.g. from a DOM ready handler).

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

2 Comments

Bro I added the name in div.ajax-response with jquery and the dom is not reloaded. like this = $('div.ajax-response').html(response); now will it find the 'p'?? It's not working in my case.... Can you tell me more??
@BlackReaper Have you put the code I provided inside a document ready handler? learn.jquery.com/using-jquery-core/document-ready
0

Yes you can. After you add the response to the document you can do stuff like:

$('.ajax-response p').click(function(ev) {
    var clickedEl = $(ev.currentTarget); 
    // do stuff
});

3 Comments

inspect DOM structure after adding the response. Then paste it here. You should do that click handler after adding!
That was my problem... don't know how to inspect DOM structure... can you tell me the method??
assuming Windows, press Ctrl+Shift+C (or F12)

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.