HyperLink text property undefined

Discussion in 'Javascript' started by avanti, Jan 2, 2007.

  1. avanti

    avanti Guest

    HI,
    I have a HyperLink in my code. It has a OnClick function defined. I am
    trying to access the text property of the HyperLink in my JavaScript
    function.

    <CoreCtrl:Controls.WebControls.HyperLink id="securityCodeLink" text="My

    Text" OnClick="ShowText(this);" Href="#" CssClass="textSmall"
    runat="server" />

    function ShowText(obj)
    {
    alert(obj.text);
    }

    I also tried getting a var for the hyperlink with its id and showing
    its text. It says 'undefined' instead of 'My Text'. Am I doing
    something wrong?

    Thanks,
    Avanti
     
    avanti, Jan 2, 2007
    #1
    1. Advertisements

  2. There is no default property called "text" for anchor elements. You
    should use innerHTML as follows:

    function ShowText(obj) {
    alert(obj.innerHTML);
    }
     
    Chad Burggraf, Jan 2, 2007
    #2
    1. Advertisements

  3. avanti

    Jim Guest

    Avanti,
    You can determine the text attribute value in this manner:
    <a href="#"
    text="sometext should go here"
    onClick="alert(this.getAttribute('text'))"Show the Text Attribute Value
    </a>

    Hope that helps,

    Jim
     
    Jim, Jan 2, 2007
    #3
  4. avanti

    avanti Guest

    Thanks guys for the quick reply. It worked!
     
    avanti, Jan 2, 2007
    #4
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.