Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-10-2013, 07:45 PM   PM User | #1
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
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
surreal5335 is offline   Reply With Quote
Old 01-10-2013, 07:57 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 01-10-2013, 07:57 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 01-10-2013, 09:32 PM   PM User | #4
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
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.
surreal5335 is offline   Reply With Quote
Old 01-10-2013, 09:49 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


Advertisement
Log in to turn off these ads.