Javascript Clickable Div

Discussion in 'JavaScript' started by rochow, Mar 28, 2010.

  1. #1
    I want to make an list item clickable with JS. Here is the markup:

    
    <ul id="services">
    <li>
    <h2><a href="#">Transformers</a></h2>
    <img src="images/transformer.jpg" alt="" />
    <h3>Sub Heading</h3>
    <p>content ... <a href="#">Read more</a></p>
    </li>
    </ul>
    Code (markup):
    There's a few different list items. I'm looking for a degradable, non jquery method (which is too hard then it should be to find!)

    The whole list item should be clickable, and preferably be hooked with a class of "current" or similar so the styling on hover can be edited.

    For how the code could work, basically for every list item inside #services, apply a class to the li on hover, and make the whole list item a link (grabbing the href from the link inside transformers).

    Ideas?
     
    rochow, Mar 28, 2010 IP
  2. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why don't you just wrap the whole listing with an 'a' tag? Then the whole listing block is clickable...
     
    Nyu, Mar 29, 2010 IP
  3. rochow

    rochow Notable Member

    Messages:
    3,991
    Likes Received:
    245
    Best Answers:
    0
    Trophy Points:
    240
    #3
    That's not valid HTML.
     
    rochow, Mar 29, 2010 IP
  4. john.michael.kane.kane

    john.michael.kane.kane Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    in css : a and a:hover....
     
    john.michael.kane.kane, Mar 29, 2010 IP
  5. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Oh i did not know. Then i would suggest to add a onlick event to the li which calls a functuion which changes the location to the a href value ;)

    Function example:
    
    function click_link(link_id) {
    	location.href = document.getElementById(link_id).href;
    }
    
    Code (markup):
    just add onclick="click_link('LINK_ID')" to the li and give the a tag the appropriate id
     
    Nyu, Mar 29, 2010 IP
  6. dgryan

    dgryan Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    onClick is the devil.
     
    dgryan, Mar 30, 2010 IP
  7. ps3ubo

    ps3ubo Peon

    Messages:
    204
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Basicly use this:
    <script type="text/javascript">
    function pageRedirect( href )
    {
    window.location = href;
    return false;
    }
    </script>

    <div onClick="pageRedirect('http://www.mp3drug.com/');" style="background: #0000FF; color: #FFFFFF; width: 300px;">
    Click anywere in this div! - Goes to http://www.mp3drug.com/
    </div>

    I added background colours etc so you can see where to click to redirect :)
     
    ps3ubo, Apr 8, 2010 IP
  8. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #8
    with class ul.over applied on mouseover:
    
    var linkifyULs = function(parent, classOn, classOff) {
        var parent = parent || document;
        var classOff = classOff || false;
        var uls = parent.getElementsByTagName("ul");
        
        if (!uls.length)
            return false;
        
        for (var ii = 0; ii < uls.length; ++ii) {
            var url = uls[ii].getElementsByTagName("a")[0].getAttribute("href");
            uls[ii].onclick = function(e) {
                window.location.href = url;
            };
            
            if (classOn) {
                uls[ii].onmouseover = function() {
                    // to be able to restore old class on mouseout if not specific as classOff as argument...
                    this.setAttribute("rel", (!classOff) ? this.className : classOff);
                    this.className = classOn;
                };
                uls[ii].onmouseout = function() {
                    this.className = this.getAttribute("rel");
                }
           }
        }
    };
            
    linkifyULs(document, "over");     
    
    Code (javascript):
    demo: http://www.jsfiddle.net/n6hNQ/2/

    keep in mind that you will get event bubbling this way - if a user clicks on the second link, the click also bubbles to the event handler, which won't be a problem if both links are the same...
     
    Last edited: Apr 8, 2010
    dimitar christoff, Apr 8, 2010 IP
  9. rochow

    rochow Notable Member

    Messages:
    3,991
    Likes Received:
    245
    Best Answers:
    0
    Trophy Points:
    240
    #9
    Perfect, thanks! Both links are the same so it's all good :)
     
    rochow, Apr 8, 2010 IP