Name to ID

Discussion in 'jQuery' started by lottevanbreugel, Feb 25, 2012.

  1. #1
    Hi,


    this is my script:



    <form id="frm1"><fieldset>
    <ul>
    <li>
    <label for="name">Name:</label>
    <input type="text" name="name" />
    </li>
    <li>
    <label for="email">Email:</label>
    <input type="text" name="email" />
    </li>
    <li>
    <label for="one">one:</label>
    <input type="text" name="one" />
    </li>
    <div id="results"><div>
    <li>
    <input type="submit" value="submit" />
    </li>
    </ul>
    </fieldset>
    </form>


    jQuery(document).ready(function($){
    $("#frm1").validate({
    debug: false,
    rules: {
    name: "required",
    email: {
    required: true,
    email: true
    }
    },
    messages: {
    name: "Please let us know who you are.",
    email: "A valid email will help us get in touch with you.",
    },



    });
    });


    The script works fine, but I would like to change something.


    I would like this:
    <input type="text" name="name" />


    to change into this:
    <input type="text" id="name" />


    But when I do this, the validation script does not work.
    Is there a way to change this?


    Thank you!
     
    lottevanbreugel, Feb 25, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    If your validation is looking for the element by name you have to change it to look for the element by ID.
     
    Rukbat, Feb 25, 2012 IP
  3. lottevanbreugel

    lottevanbreugel Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you, do you know how I can do that?
     
    lottevanbreugel, Feb 25, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    That depends on what you're using to validate the form, the jQuery validator plugin or something else. The plugin should work on ids as it is (I've never used it, but the documentation seems to indicate that it'll validate the entire form).
     
    Rukbat, Feb 26, 2012 IP
  5. Moxie Technoxy

    Moxie Technoxy Peon

    Messages:
    11
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Why can't you just do this:

    [COLOR=#111111]<input type="text" [/COLOR]name="name" id="name" />
    Code (markup):
    And have both the name AND id in there?
     
    Moxie Technoxy, Mar 15, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    You can, but if you need only one of them you're just increasing the amount of cruft the user has to download. Use the one that the code needs. (There's almost never any reason for the code to need both unless it's very badly written.)
     
    Rukbat, Mar 15, 2012 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Excuse me? the "name"-attribute is REQUIRED for either post or get-functionality to work (we're not talking javascript here) - tha ID is an identifier, albeit it's not really NEEDED anymore for anything, it's still a different attribute than "name", and those two (apart from in javascript, and to some extent CSS) are NOT interchangable.

    If, for instance, he want to validate form values using jQuery, he can use either, same if he submits the form via jQuery ($.post or whatever) - however, if the user have turned OFF javascript, for instance, he would want a reliable fall-back, and hence, should use the "name"-attribute. If you remove the name-attribute, the HTML is no longer valid, btw.
     
    PoPSiCLe, Mar 27, 2012 IP