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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting a href from an "A" element

    Hello,

    I've a problem, I want to get the 'href' from a text by using javascript.
    But I only want to see the 'href' when my cursor is on it.. so if my cursor is not between the <a></a> I don't want to get the href..
    I'm getting confused of it, and I hope someone knows a solutions for it..
    this is what I have so far:

    my NOT working code to get the href:
    Code:
    document.selection.createRange().getElementsByTagName("A").href
    code where I want get out of the href
    Code:
    <div>
    bla bla bla
    <a href="www.site.nl">Text1</a>
    efefefef
    <a href="www.blabla.nl">Text2</a>
    blalbla2
    </div>
    so when my cursor is on 'Text1' I want to see the href of it, but when my cursor is on 'Text2' I want to see the href of that link.

    Regards,


    Harwin

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    I suppose that this will work, I'm not sure:

    Code:
    document.selection.createRange().getElementsByTagName("A").innerHTML
    Edit:
    I figure out that this can't work, becouse:
    - probably must be
    Code:
    document.getElementsTagName('A')
    - getElementsByTagName return an array
    - I don't know how to use selection and createRange but I guess that must be used somewhere after getElementsByTagName
    - innerHTML and href must be apply to a single element of the array

    sorry, maybe somebody could help you more then I can do


    best regards
    Last edited by oesxyl; 01-31-2008 at 11:27 PM.

  • #3
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the fast reply, but that isn't working for my script...

    it's not a problem to get the href out of this:

    <a href="www.test.com">text</a>
    on that moment I can use this:
    Code:
    document.selection.createRange().parentElement().href
    but it's a problem when &#237;t is like this:
    <a href="www.test.com"><strong><font>text</font></strong</a>

    which code do I need to use to get the href out of it..
    I hope someone else can help me

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    If you can, put a unique id to that <a>
    Code:
    <a id="link1" href="www.test.com">text</a>
    Then
    Code:
    document.getElementById('link1').href
    will work.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes that would be an easy way.. but it isn't that easy,

    I don't want to use the 'id' tag.
    I want to use it for a WYSIWYG script, there I want to display the 'href'.

    but thanks

    someone else haves experience with it? Or any idea's how to fix it?

  • #6
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I found a little solutions for this:

    Code:
    if (undefined != document.selection.createRange().parentElement().href){
    	var showUrl = document.selection.createRange().parentElement().href;}
    	else if (undefined != document.selection.createRange().parentElement().parentElement.href){
    	var showUrl = document.selection.createRange().parentElement().parentElement.href;}
    	else if (undefined != document.selection.createRange().parentElement().parentElement.parentElement.href){
    	var showUrl = document.selection.createRange().parentElement().parentElement.parentElement.href;}
    thanks for your reply's!
    if someone knows a better way to do this, let me know and if not, than this topic can be closed.

    Regards,


    Harwin

  • #7
    New Coder
    Join Date
    Nov 2007
    Location
    Thane-Vashi,Mumbai,India.
    Posts
    44
    Thanks
    3
    Thanked 2 Times in 2 Posts


  •  

    Posting Permissions

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