2

I need to add another input field of type select if someone clicks add location as seen in my code

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p><strong> location</strong></p></br />
<select name="location">
    <option value="location1">location1</option>
    <option value="location2">location2</option>
    <option value="location3">location3</option>
    <option value="location4">location4</option>
</select>
<a href="#" id="#newField">Add location</a>
<hr/>
</form>

I have a few more selects in the form tag but here is where i have a problem. i need to just add the field and not refresh any of the the data the person has added.Thankinging you for your replies.

3
  • 2
    Do you mean an option should be added? What should be it's value and what this has to do with php? Commented Sep 7, 2012 at 6:47
  • Do u need to add the select option using jquery??? Commented Sep 7, 2012 at 6:50
  • i need the entire select to be added just below this already existing select. Commented Sep 7, 2012 at 6:51

1 Answer 1

4

You can use clone:

$('#newField').click(function(){
    $('select[name=location]:first').clone().insertAfter('select:last')
})

Fiddle

Note that you should not use # in your id attributes.

<a href="#" id="#newField">Add location</a>
         //-----^
Sign up to request clarification or add additional context in comments.

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.