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 4 of 4
  1. #1
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Retrieving Label textNode?

    I know how to use the DOM to create a label:

    Code:
              var label = document.createElement('label');
                  label.setAttribute('for','firstName');
                  label.appendChild(document.createTextNode('New Name'));
    But I am trying to retrieve the value of a form elements label textNode value... I need to do this without using an id on the label and was hoping to do this thru the input itself...

    Although if I wrap the input within the <label><input></label> I can retrieve the textNode thru parentNode.innerHTML... But I need to do this when the label is in another data cell and thus does not share the same parentNode....

    Code:
        <form>
         <table>
          <tr><td><label for="firstName">First Name: </label></td></tr>
          <tr><td><input type="text" id="firstName" name="firstName"
                   onclick="alert(this.label.getAttribute('for').value)"></td></tr>
         </table>  
        </form>
    I've searched and searched without joy...
    Any ideas?


    .....Willy
    Last edited by Willy Duitt; 10-04-2004 at 08:57 PM.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The fastest solution is probably to use document.getElementsByTagName method to get all label elements, and then search the htmlFor property of each of them until you find one that matches the id of your form control.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, I'll try that but I was hoping to do this more directly....

    Once, I thought I did something with a camelCase labelFor getAttribute but can not find a copy of that script....



    .....Willy
    Last edited by Willy Duitt; 10-04-2004 at 09:50 PM.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Well, if you want better efficiency you can create a static lookup table, under the highly likely circumstance that you don't dynamically add or remove label elements to the document.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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