Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    get value of name attribute

    so I have an object of an input element which I am trying to get the value of its name attribute. From here I am looking to get the value of the text in the input field itself. I guess I could always skip straight to grabbing the value of the input, but ohwell

    Here is the code I have: so far my alert() is returning undefined

    Code:
    var patt = new RegExp('/^[A-Za-z0-9@.]$/',modifiers);
            var ck_input = /^[A-Za-z0-9@.]$/;
            function validateText(input_id){
                var name = input_id.getAttribute("name").value;
               // var input = document.getElementsByName(name).value;
                alert(name);
                /*
                if(!ck_input.test(input)){
                    alert('please input alphabetical and numerical characters only');
                }
                */
            }
    you will noticed code has been commented. I am unit testing before moving onto validating the value with regEX Any ideas on how to reach the end goal would be great.

    Thanks a lot

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,333 Times in 4,299 Posts
    This code makes no sense.

    The name of an <input> field is simply a string.

    So when you do input_id.getAttribute("name") that gives you a string.

    You can *NOT* then get the .value attribute of a string. Strings don't have a .value attribute.

    On top of that, the name of an <input> field is determined by the HTML coder. As in:
    Code:
        <input type="text" id="zamboni" name="widget" />
    So the person *using* the HTML page has NO CONTROL WHATSOEVER over that name. It is and will be whatever the person who coded the HTML assigned it to be.

    I think you are very very badly confused.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,333 Times in 4,299 Posts
    Maybe you should describe, in words, what you are trying to do. Don't show us random code. Just describe the problem.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    The point of .getAttribute() is to get the specified attribute of the html element in question. I looked it up and found that it gets the value of it already so I can drop the .value portion of it. I am used to defining .value for these kinds of things to get the value.

    I am not trying to validate the value of the name attribute, I am trying validate the value of the input element. I have lots of input elements and I need to identify which input has been targeted, thats why I get the name attribute first.


    // var input = document.getElementsByName(name).value;

    currently its commented out, but I will be uncommenting it so as to get the value of the input field after targeting it with the getElementsByName(name). After this I would validate.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,333 Times in 4,299 Posts
    Code:
       var input = document.getElementsByName(name).value;
    Still very wrong.

    getElementsByName() returns an *ARRAY* of all elements on the page with that name.

    Even if there is only one by that name.

    So if you *KNOW* there is only one by that name:
    Code:
    var input = document.getElementsByName(name)[0].value;
    But that fails miserably if, for example, you have a set of 5 checkboxes all with the same name.

    **********

    Almost always, a better way to do this is to use the <form> to reference its member <input>s.

    Example HTML:
    Code:
    <form id="myForm">
        <input name="username" /><br/>
        <input name="address" /><br/>
        <label><input type="radio" name="gender" value="m" checked> Male</label>
        <label><input type="radio" name="gender" value="f"> Female</label>
    ...
    </form>
    Then you can get fields in the form by name *MUCH* more simply. Example:
    Code:
        var form = document.getElementById("myForm");
        var usernameValue = form.username.value;
        var addressValue = form.address.value;
        var genderValue = form.gender[0].checked ? "m" : "f";
    And so on.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •