Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: hyperlink a div

  1. #1
    Join Date
    Nov 2005
    Posts
    132
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default hyperlink a div

    Hi

    Any pointers on assigning a hyperlink to a div layer? Basically I have a square div with a embedded background pic which I want to to clickable.

    thanks in advance.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Does the image really need to be the background? That seems weird.... you could just put the image IN the div and be done with it.

    Just tried this; it works. Note that it does require javascript, though. Don't think there's another way.
    <div onClick="document.location='yoururl';"></div>
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Nov 2005
    Posts
    132
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    ok, cheers. Maybe best to work round the js and insert the image.
    thanks again

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Yeah.... if there's no need for the image to be background (like having text over it, etc.), then that would be the best, most compatible option, as well as being easy.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    document.location?
    I think you mean window.location.href.

    Anyway... you can't put a DIV inside an A, because A is an inline element while DIV is a block-level element. djr33's solution, while a tad off on the specifics, is the right idea.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    HTML Code:
    <div><a href="whatever.htm">link text</a></div>
    That's the right way to do it but, of course only the link text will be active as a link.

    If you give the div a set width and height in its style and set the the a tag's style to width:100%;height:100%;display:block; things should work out pretty well, some tweaking of the style may be required to align the link text (if any, a nonbreaking space - &nbsp; - should at least be used) as desired.

    If you were to give a concrete example, I could work out the details. The division tag may not be required at all.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The document.location worked just fine.
    If window.location.href is better, by all means use that.

    (Why does document.location work, then? 'cause it worked in a couple browsers, I think... didn't fail in any that I tested... can't remember which I used to test, though.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    (Why does document.location work, then? 'cause it worked in a couple browsers, I think... didn't fail in any that I tested... can't remember which I used to test, though.)
    Don't know... I'd never heard of it After some testing, though, it seems that document.location is always exactly the same as window.location. While you should still be using document.location.href, then, I guess there's no harm.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey
    Don't know... I'd never heard of it After some testing, though, it seems that document.location is always exactly the same as window.location. While you should still be using document.location.href, then, I guess there's no harm.
    This is the HTML forum, anything.location is not a solution to this problem.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    But your method creates an annoying character filling the div. What if he needs content to be inside the div?

    I agree it's better to use a non-JS solution, but since there is no default way to link a div, I think JS is better than faking a filled div, because that would just mean you could put the image in it, not as a background, and there must be some reason he's not doing this in the first place.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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