How can I extract this table cell value?

Discussion in 'Javascript' started by D. Alvarado, Oct 10, 2004.

  1. D. Alvarado

    D. Alvarado Guest

    Will Javascript allow me to extract the value 'NWEINETTE' (value from
    first td) from the following HTML?

    <tr id="lightrow" class="data">
    <td class=data id='USER_NAME' nowrap><A class='data'
    HREF="user_profile.jsp?LOGIN=NWEINETTE">NWEINETTE</A>&nbsp;&nbsp;&nbsp;</td>
    <td class=data id='FIRST_NAME' nowrap>nancy&nbsp;&nbsp;&nbsp;</td>

    <td class=data id='MIDDLE_NAME' nowrap>f&nbsp;&nbsp;&nbsp;</td>
    <td class=data id='LAST_NAME' nowrap>weinette&nbsp;&nbsp;&nbsp;</td>
    </tr>

    Ideally, it would be great if I could find some cross-browser (IE 6+,
    Netscape 7+) JS that could do the task.

    Thanks, - Dave
     
    D. Alvarado, Oct 10, 2004
    #1
    1. Advertising

  2. D. Alvarado

    asmo Guest

    Hi Dave,

    try this:

    // get a ref to the row
    var theRow = document.getElementById("lightrow");
    // get the first link
    var theLink = theRow.getElementsByTagName("A")[0];
    // your value
    alert(theLink.innerHTML);

    this should work in any dom compliant browser..

    regards
     
    asmo, Oct 10, 2004
    #2
    1. Advertising

  3. On 10 Oct 2004 06:11:32 -0700, asmo <> wrote:

    Please quote relevant text from the post you are responding to, including
    who wrote that post.

    > // get a ref to the row
    > var theRow = document.getElementById("lightrow");
    > // get the first link
    > var theLink = theRow.getElementsByTagName("A")[0];
    > // your value
    > alert(theLink.innerHTML);
    >
    > this should work in any dom compliant browser..


    Why? The innerHTML property isn't defined as part of the DOM, though it is
    supported by the "major" browsers.

    A solution that is fully DOM-compliant is:

    var user = document.getElementById('USER_NAME');
    // value:
    user.firstChild.firstChild.data

    This makes a couple of assumptions:

    1) The id, USER_NAME, is unique. Whilst this should be the case anyway as
    id values must be unique, I don't get that impression from the HTML
    provided by the OP.
    2) There is no whitespace between the opening cell and link tags. If there
    is whitespace, the code becomes a little more involved, but simple
    nevertheless.


    On 9 Oct 2004 21:47:16 -0700, D. Alvarado <>
    wrote:

    > <tr id="lightrow" class="data">
    > <td class=data id='USER_NAME' nowrap><A class='data'
    > HREF="user_profile.jsp?LOGIN=NWEINETTE">NWEINETTE</A>&nbsp;&nbsp;&nbsp;</td>


    If you need to pad an element, use the CSS padding property. With the
    markup you presented, the rule

    #lightrow td {
    padding-right: 3ex;
    }

    would achieve what the non-breaking spaces do, but with the benefit of not
    being a hack. A different selector might be more appropriate, depending
    upon the rest of your page.

    A small request when posting code: please don't use tabs. They can cause a
    significant amount of wrapping. Instead, use spaces - two is probably be
    the best, considering the limited width.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Oct 10, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Dixson
    Replies:
    1
    Views:
    541
    Joe Fallon
    May 18, 2004
  2. Phillip Roncoroni
    Replies:
    14
    Views:
    1,135
    Toby A Inkster
    Apr 5, 2004
  3. Empire City
    Replies:
    3
    Views:
    431
    Empire City
    Apr 30, 2004
  4. RJ
    Replies:
    1
    Views:
    258
    Eliyahu Goldin
    Feb 17, 2005
  5. Replies:
    7
    Views:
    214
    Lasse Reichstein Nielsen
    Jul 11, 2005
Loading...

Share This Page