David
David

Reputation: 4518

jquery accessing form element after ajax reply

I'm sending form to server and when I receive it back I need to access its elements

Here goes code.

some html

<here is the form>
<div style='border-style:solid' id='user_pic_n_1'></div>

and javascript

pic_type=($(this).attr('pic_type'));                    
pic_number=($(this).attr('pic_number'));

explanation : pic_type gets > user_pic and pic_number gets > 1

so the form is user_pic_form_n_1

$('#'+pic_type+'_form_n_'+pic_number).ajaxSubmit({
    success: function(responseimage)
    {
$('#'+pic_type+'_n_'+pic_number).html(responseimage);

now when the form gets into div, I need to get it's inputs values.

please write code sample of how can I Access it

In my example i'm trying to get values by alert like this but probably I write with mistakes

alert($(\"#'+pic_type+'_form_n_'+pic_number+' input[name=error]\").val());

elements name is 'error' so I'm trying to get it from the form.

UPDATE

Here is HTML form which I get from AJAX

<form id='user_pic_form_n_1' name='user_pic_form_n_1'   action='/serv/pic_processor.php' method='POST'>
<input type='hidden' name='error' value='456'/>
</form>

So when it gets from server into responseimage variable, I put that into Div and then I want to access this form and alert value of tag named 'error'

Upvotes: 1

Views: 472

Answers (2)

Adil
Adil

Reputation: 148178

You can pass the element contain the response html as a context along with selector.

selectorElem = '#'+pic_type+'_n_'+pic_number;
$(selectorElem).html(responseimage);
$('form[name=A] #B', $(selectorElem))

Edit, based on comments

Live demo

Html

<form id='user_pic_form_n_1' name='user_pic_form_n_1'   action='/serv/pic_processor.php' method='POST'>
<input type='hidden' name='error' value='456'/>
</form>​

Javascript

pic_type = 'user_pic';
pic_number= '1';
selectorElem = pic_type+'_form_n_'+pic_number;
selectorElem = 'form[name='+selectorElem +'] :hidden[name=error]';
alert($(selectorElem).val());

Upvotes: 1

Rahul garg
Rahul garg

Reputation: 9362

After you have done this:

$('#'+pic_type+'_n_'+pic_number).html(responseimage);

and you are looking for val of #b inside form with class a, it would be as:

$('#'+pic_type+'_n_'+pic_number+ ' form.a #b').val()

Upvotes: 0

Related Questions