Read Value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laskowski@gmail.com

    #1

    Read Value

    How can I determine the value TEST from both of the scenarios below?
    For the <P> tag I can use OMON_3.innerTex t, but with the <Input> tag I
    need to use OMON_3.value. However, in my application depending on how
    the page is viewed, either the <P> tag will be displayed or <Input>.
    Is there a way to find the value in both cases with the same piece of
    code?

    <P id="OMON_3">TES T</P>

    <INPUT style="width:13 5px" maxlength="15" name="OMON_3" tabindex="4"
    value="TEST" />

  • Evertjan.

    #2
    Re: Read Value

    wrote on 03 mrt 2005 in comp.lang.javas cript:
    [color=blue]
    > How can I determine the value TEST from both of the scenarios below?
    > For the <P> tag I can use OMON_3.innerTex t, but with the <Input> tag I
    > need to use OMON_3.value. However, in my application depending on how
    > the page is viewed, either the <P> tag will be displayed or <Input>.
    > Is there a way to find the value in both cases with the same piece of
    > code?
    >
    > <P id="OMON_3">TES T</P>
    >
    > <INPUT style="width:13 5px" maxlength="15" name="OMON_3" tabindex="4"
    > value="TEST" />[/color]

    Why not use an id in both instances, like:

    <INPUT name="OMON_3" id="OMON_3" value="TEST">

    =============== ========

    but to have it your way:

    <script type='text/javascript'>
    if (document.getEl ementById('OMON _3').value)
    alert(document. getElementById( 'OMON_3').value )
    else
    alert(document. getElementsByNa me('OMON_3')[0].innerHTML);
    </script>

    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)

    Comment

    • Michael Winter

      #3
      Re: Read Value

      laskowski@gmail .com wrote:
      [color=blue]
      > For the <P> tag I can use OMON_3.innerTex t,[/color]

      If IE is the only browser to be exposed to this script, then yes you can.
      [color=blue]
      > However, in my application depending on how the page is viewed,
      > either the <P> tag will be displayed or <Input>. Is there a way to
      > find the value in both cases with the same piece of code?[/color]

      Once you have a reference to an element, you can determine the type of
      element using either the tagName (for HTML documents) or nodeName (for
      either XML-based or HTML documents) properties.

      However, you're going to have difficultly getting that reference at
      all with the mark-up you've posted: the P element can be obtained
      using the getElementById method, however the INPUT element doesn't
      have an id so it can't[1]. Generate an id attribute for both elements
      and you can use something like:

      var e = document.getEle mentById('OMON_ 3'),
      v = '';

      if(e) {
      if(('P' == e.tagName) && e.firstChild) {
      v = e.firstChild.da ta;
      } else if('INPUT' == e.tagName) {
      v = e.value;
      }
      }

      [snip]
      [color=blue]
      > <INPUT style="width:13 5px" maxlength="15" name="OMON_3"
      > tabindex="4" value="TEST" />[/color]

      For an XML-based document, the strings used to compare tag names will
      need to be lowercase, and the property changed from tagName to
      nodeName. That said, despite the XHTML-like syntax used above, you
      clearly aren't using XHTML as HTML-adopted tag names must be
      lowercase. Remove the forward slash (/) from the end of the tag.

      Hope that helps,
      Mike


      [1] The behaviour exhibited by IE where getElementById will retrieve
      an element reference by either id or name is erroneous and it
      should not be relied upon. Whichever Microsoft decision maker
      thought that one up is an idiot!

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • laskowski@gmail.com

        #4
        Re: Read Value

        Perfect! I know the structure seems a little odd, but it is what I
        have to work with. Thank you for the help!

        Comment

        Working...