473,394 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to give a unique id to each input field created with button click

please i have a problem,i create new input field when a button is click,but i want each of the new create field to have a unique id,so i can use it for calculation in javascript.

Expand|Select|Wrap|Line Numbers
  1. function create(){
  2.  
  3.             var newinput = document.createElement('input');
  4.             newinput.placeholder = "test1";
  5.             newinput.id = 'test1'; 
  6.                 document.getElementById("mytest").appendChild(newinput);
  7.             }
May 4 '17 #1
4 1659
Dormilich
8,658 Expert Mod 8TB
solution: don't use IDs for your calculation but classes. If you're having an unknown number of input fields, you can't base your calculation on IDs anyways.
May 4 '17 #2
but i don't know how to do that can you edit my code and fix it
May 4 '17 #3
Dormilich
8,658 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. function create()
  2. {
  3.     var newinput = document.createElement('input');
  4.     newinput.placeholder = "test1";
  5.     document.getElementById("mytest").appendChild(newinput);
  6. }
May 4 '17 #4
jmrker
17
As 'Dormilich' said, I would not recommend this approach for calculations, but it is not impossible to do and can be done like this...
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title> Create Inputs </title>
  6. </head>
  7. <body>
  8. <button onclick="countThem()">Count Them</button>
  9. <div id="mytest"></div>
  10.  
  11. <script>
  12. function create(xx) {
  13.   var newinput = document.createElement('input');
  14.   newinput.placeholder = "test"+xx;
  15.   newinput.id = 'inp'+xx;
  16.   document.getElementById("mytest").appendChild(newinput);
  17.  
  18.   newinput = document.createElement('br');
  19.   document.getElementById("mytest").appendChild(newinput);
  20.  
  21. }
  22.  
  23. function init() { for (var i=0; i<10; i++) { create(i); } }
  24.  
  25. init();
  26.  
  27. function countThem() {
  28.   var sel = document.querySelectorAll('input');
  29.   var str = '';
  30.   for (var i=0; i<sel.length; i++) { str += sel[i].id+'\n'; }
  31.   alert(sel.length+'\n\n'+str);
  32. }
  33. </script>
  34.  
  35. </body>
  36. </html>
  37.  
Jun 4 '17 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: chris | last post by:
hi all im relativly new to PHP but loving it - its fantastic isnt it (reminds me a lot of BASIC on steroids) my question is this how do i call a function from a button using php?? Thanks
2
by: Rosebud | last post by:
Hello, I'd like some help trying to generate input fields on the fly. What I have on an HTML page is a single text input field labelled #1, e.g. #1 <input type="text">. Next to the field is a...
2
by: Ron Aronica | last post by:
I want to specify the value of an input field using the return from a function. I want to do something this this: <script> function value() { return "the value"; } </script> <form>
2
by: Martin Knott | last post by:
Hopefully an easy answer. I've got a form with 1 input field, and a submit button. I register an event for the button, run in debug mode, push the button and sure enough I'm in the event...
1
by: Adrian Parker | last post by:
v1.1 - After taking off smartnav (due to it not working properly), I've got code that will restore the scroll position of the page on postback, but now I'm looking at how to set focus on either a...
17
by: Eric | last post by:
I'm new to JavaScript and I wrote this code to play with. Oddly, if I enter text in a box and then press the button, I only get the onChange event for the text box and not the button's onclick...
7
by: =?Utf-8?B?bWFydGluMQ==?= | last post by:
Hi, All, I create button in the code ( Dim Button as new Button), not using button web component (means not drap button and drop it ont he webform), after that I try to use button_click event,...
3
by: yimma216 | last post by:
I hava a form, which has five lines to enter at start. I want to have a button to add more lines when it is clicked. However, I am having trouble with it. This would be a form in a form using vbscipt...
1
by: cluless | last post by:
Hey everybody, I have a database with one major form that has a few subforms and a listbox, and on the form there are buttons that link to other forms as well. Once the list box is clicked on, the...
3
by: Jeff Karli | last post by:
I want to confirm a number was entered before allowing the click to proceed. The click does work on its own but when I add the first If statement below it does not display the message....however when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.