September 18, 2012

Multiple Input fields with same id

Question by fahad

I am rendering data from php code with a input field but when i retrieve data from database the input field is replicated and i have assigned one id and one class value to it now i am facing that if i use id it only show me the 1st value only.if i use class it start iterating it and complete till each field and show empty where input fields are empty

Jquery Code is this

    $('.submit-button').on('click',function(){
       $('.datecontrol').each(function(){
       alert($(this).val());
   });

and my whole html is provided here

Answer by Starx

First, as per the title, it is not a good idea to use multiple fields with same id. It will create problems is CSS, Scripting, etc.

Next, if selecting certain numbers of field only is your problem, then assign a different class to the fields to represent a particular group.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!