0

I am appending input field by jQuery append and trying to get the value of of it when submiting the form. Please see a working version in JSFiddle. When I click on submit button, I am geting undefined. Appreciate your help. Thanks!

http://jsfiddle.net/XRUX8/

$('[name=addAgent]').bind('click', function() {

    var agentName = $("#Test1").val(); 
    var testAgentIDVal = "A12345";



            if($('#container').find('[name=removeAgent]').length < 2) {

                var len = $('#container').find('[name=removeAgent]').length;
                //alert(len);
                var index = len+1;

                $('#container').append('<label>'+agentName+'</label><input type="hidden" id ="AgentID"'+ index +'  name ="AgentID"'+ index +'  type="text" value="'+testAgentIDVal+'" ><button type ="button" name="removeAgent" id="removeAgent"  >Remove Agent</button>');


            }
        })

$('[name=tab1Submit]').click(function(){

alert($("#AgentID1").val() +", "+ $("#AgentID2").val());

})

2 Answers 2

2

instead of:

id ="AgentID"'+ index +'  name ="AgentID"'+ index +'

it should be

id ="AgentID'+ index +'"  name ="AgentID'+ index +'"
Sign up to request clarification or add additional context in comments.

1 Comment

STill i see the same error. Could you please update the fiddle? Thanks!
1

When you look at the html generated you will see your mistake

<input type="hidden" id="AgentID" 1 name="AgentID" value="A12345">

You have to change

id ="AgentID"'+ index +' name ="AgentID"'+ index +'

To

id ="AgentID'+ index +'" name ="AgentID'+ index +'"

2 Comments

Got it Great!! Thank you!. BTW, I am using IE, can you please help me how to see the HTML code for my screens? If I rightclick and click view source, I could not see the HTML code that was added by jQuery append. Here is updated fiddle. jsfiddle.net/XRUX8/2
In IE you have something called Developer tools you can access this by pressing F12, This will allow you to see a new window and new set of options. If you make changes to the DOM make sure you refresh the tool so it can pick up the changes you made otherwise the new changes will not be visible, this is done by a up and down blue arrows.

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.