Changing Input Type, length and value

Discussion in 'Javascript' started by Boyd Reilly, Jan 11, 2004.

  1. Boyd Reilly

    Boyd Reilly Guest

    I have a form that has the user pick the type of question he will answer.
    The input field will be a text, numeric or date type. So, after the
    question is answered, I need to change the input statement. This resembles
    what I am doing:

    <form name="frm1">
    Step 1 - Choose a File: <select name="selA" OnChange="makeinputbox;"
    width="200" style="width:200">
    <option value="">Date of Birth</option>
    <option value="MASTER">Age</option>
    <option value="ADDRESS3">Maiden Name</option>
    Step 2 - Insert Value: <INPUT TYPE="text" NAME="MySingleLineTextBox"

    <script language="JavaScript">

    function makeinputbox {
    // -->


    The only thing that happens is that the value gets set to 1, not 1.00. The
    input bo still keeps all of its original characteristics.

    Any ideas what I need to do?


    Boyd Reilly, Jan 11, 2004
    1. Advertisements

  2. Boyd Reilly

    Boyd Reilly Guest


    Thanks for he help. If you haven't already noticed, I'm a bit new to java
    scripting. If you could, please explain the difference between <script
    language="JavaScript"> and <script language="text/javascript">.

    Also, I have been learning java scripting, mostly, through example. You say
    type is read-only. Is there a way to change it to a date or numeric type
    format? Basically, I want to restrict users to only a numbers or a date
    format. I guess if it is really a read-only function I'm going to have to
    develop a different method of attacking this problem - suggestions are


    Boyd Reilly, Jan 11, 2004
    1. Advertisements

  3. And Usenet too by the look of it.

    JavaScript (one word) is not scripting with Java. Java is a distinct
    language (and in most respects a very different language). The name was
    probably a mistake as it causes a lot of confusion, but it is too late

    There is a longstanding convention on Usenet that messages are formatted
    so that only material that is being responded to is quoted and the text
    that represents the response appears under the quoted text that is being
    responded to. You have quoted the entire message you are responding to
    verbatim (and badly line wrapped) and have placed your reply above it. A
    practice known as "Top Posting" and strongly discouraged.

    It is often proposed that there is a correlation between top posting and
    evidence in the top posted text of a failure to read the material that
    has been posted over.

    Another Usenet convention has technical groups maintaining a frequently
    asked questions resource and asking new participants to read it,
    preferably prior to posting to the group but ASAP otherwise. The
    comp.lang.javascript FAQ is posted to the group on a regular bases
    (search for FAQ in subject lines) and is also available online at:-

    <URL: >

    Section 2.3 is of immediate relevance, but reading the rest (and linked
    resources) will be invaluable for a novice JavaScript author.
    Randy actually wrote - type="text/javascript" - rather than:-
    A discrepancy that you probably would have spotted if you had posted
    that line directly under a quote of his comment.

    The HTML 4.01 SCRIPT element has 7 official attributes defined in its
    (loose) DTD. TYPE and LANGUAGE are two of those. The TYPE attribute is
    listed as required by the DTD and so it must be present for an HTML 4
    document to be valid. If the script language used is JavaScript, and
    baring in mind that HTML attribute names are case insensitive, then the
    opening script tag must include:-


    On the other hand the LANGUAGE attribute is deprecated, which is
    intended to indicate that it should no longer be used. And the HTML 4.01
    strict DTD does not include the LANGUAGE attribute so a document written
    to conform to that DTD could not include it and still be valid.

    In practice these are considerations of formal correctness. Most
    browsers don't know any other scripting languages and those that do
    default to assuming JavaScript in the absence of anything to the
    contrary. Browsers are also extremely tolerant of errors and willing to
    bend over backwards to make some sort of sense out of whatever rubbish
    they are sent, so just writing "<script>" will probably have the desired

    However, there is an axiom that goes: be tolerant in what you accept and
    strict in what you send. The browsers are tolerant in what they accept
    and it is up to the web authors to be strict in what they send. Which
    means valid HTML (at least) and so the TYPE attribute in script tags and
    only the LANGUAGE attribute if the strict DTD is not used (and even then
    preferably no LANGUAGE attribute).

    But valid HTML has an advantage of its own when combined with
    JavaScript. The script wants to be able to interact with a DOM
    constructed from that HTML and there are (more or less) well specified
    rules as to what that DOM should be like when it is constructed based on
    valid HTML. The browsers might not always manage to hit the desired
    target but at least you know that they are all shooting at the same

    While a browser receiving invalid HTML will apply various "error
    correcting" rules to sort the HTML out into a DOM that makes sense. But
    those rules are particular to the individual browsers and not published
    or standardised in any way so there can be a lot of variation in the DOM
    that results. The last thing a script author want is to encourage more
    inconsistency in the DOMs that they will be working with (there is
    enough of that already).
    Be extremely cautions of example scripts found on the internet
    (especially in the form of copy'n'paste collections) you will probably
    find considerably more examples that exemplify how *not* to write
    scripts than good examples.
    There is no such thing as date or numeric format with INPUT elements.
    They accept strings of text, nothing more and nothing less. You can
    specify a maximum length for the string but that is it.
    No, that is not what you want to do. You want to provide the user with
    information about the format you would like, possibly you would also
    want to verify that what the user enters is in that format once they
    have entered it and prior to sending the information to a server for
    back-end processing (baring in mind that all client-side verification
    can be subverted so the back-end must always check that its input
    conforms to its requirements). That is standard form validation and you
    will be able to find hundreds of examples in the archives. Pay
    particular attention to Regular Expression based validation code (but
    not for all tasks).
    And Randy wrote:-


    The comp.lang.javascript archives are available at and
    google provide more ways of searching them than you will probably find a
    use for.

    Richard Cornford, Jan 11, 2004
  4. Not in DOM 2.

    It is read-only in DOM 1, but since you need to be able to set the type
    of a newly created input element (document.creatElement("input")), it
    must be writable.

    Browser uspport for DOM 2 is a different problem, ofcourse. As usual
    IE won't understand it when you change the type after setting it the
    first time.
    It doesn't exist in HTML.

    Lasse Reichstein Nielsen, Jan 11, 2004
  5. Boyd Reilly

    Jim Ley Guest

    document.implementation.hasFeature("DOM 2.0") makes no claim to
    supporting DOM 2, so you can't complain it doesn't do something it
    claims to.

    (Of course the hasFeature test is ridiculous)

    Jim Ley, Jan 13, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.