• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Input data in defined fields

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I enter the value of a empty input field using javascript?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Javier Julio wrote:How can I enter the value of a empty input field using javascript?


You'll need to be more specific; I do not understand what you are asking.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Javier Julio wrote:How can I enter the value of a empty input field using javascript?



I think he means he wants to auto populate a bunch of fields using javascript.

Try this:

HTML:


Javascript:


to auto populate the field above for example use:
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!  
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Justice wrote:
   document.getElementById(name).value = value;


It's not a good idea to rely upon the name being used as the id. This is non-standards behavior that might or might not work going forward. It's best to assign explicit id values to the elements.
 
Michael Justice
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Michael Justice wrote:
   document.getElementById(name).value = value;


It's not a good idea to rely upon the name being used as the id. This is non-standards behavior that might or might not work going forward. It's best to assign explicit id values to the elements.



You mean like this:

I only used that because it's flexible and can be applied to many text fields at once instead of having to explicitly use hard coded values which may change later on.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I mean that the element's name attribute should not be used as the id. Use explicit id values. As in:





Relying upon the name being assigned as the id is a holdover from older browsers that still exists for backwards compatibility but could become unsupported at any time. It's best to break that habit and be explicit.

I only used that because it's flexible and can be applied to many text fields at once instead of having to explicitly use hard coded values which may change later on.


I don't really see the advantage of creating a function to replace a single line of code unless it has clear readability advantages, which in this case, I do not feel apply. The hard-coded value of the id still needs to be used in the code whether a user-supplied function or the document method is used or not.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic