SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs down Help me with document.getElementsByName ???

    I write a form like:
    <form name="linh" action="1.php">
    <input type="text" name="nguyen" value="Hello word">
    </form>
    -------------
    I want to get "hello word" from this form by javascript
    I tried write:
    <script>
    var a=document.getElementsByName("nguyen");
    alert(a);
    </script>

    But my code don't run.
    Can everyone help me ?
    Thanks all !!!

  2. #2
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You need to access the form elements:

    Code:
    <form name="linh">
    	<input type="text" name="nguyen" value="Hello word">	
    </form>	
    <script>
    	var a = document.linh.nguyen.value; // document - form name - input name - value
    	alert(a);		
    </script>

  3. #3
    Pedantic Semantic blain's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason you're code doesn't work is that getElementsByName() returns a nodelist not the value.

    Code:
    var a=document.getElementsByName("nguyen");
    alert(a[0].value);
    Technology is dominated by two types of people:
    those who understand what they do not manage,
    and those who manage what they do not understand.

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oki, thanks all very much !


Bookmarks

Posting Permissions

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