CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   get value of name attribute (http://www.codingforums.com/showthread.php?t=285578)

surreal5335 01-10-2013 07:45 PM

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

Old Pedant 01-10-2013 07:57 PM

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.

Old Pedant 01-10-2013 07:57 PM

Maybe you should describe, in words, what you are trying to do. Don't show us random code. Just describe the problem.

surreal5335 01-10-2013 09:32 PM

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.

Old Pedant 01-10-2013 09:49 PM

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.


All times are GMT +1. The time now is 04:02 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.