SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2012
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get li attribute using javascript or jquery.

    Hi,

    I want to get url attribute of li when user click on li.

    Code:
    <ul>
    	<li url="http://www.google.com" id="1">Google</li>
    	<li url="http://www.yahoo.com" id="2">Yahoo</li>
    	<li url="http://www.bing.com" id="3">Bing</li>
    	<li url="http://www.facebook.com" id="4">Facebook</li>
    	<li url="http://www.amazon.com" id="5">Amazon</li>
    </ul>
    I found following but no idea how it will work

    Code:
    $('li').click(function() {
        alert($(this).data('url'));
    });
    Any idea how to get li url value when click using javascript or jquery?

    - Thanks
    Zohaib.

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    just change "data.url" into "attr.url" and you'll be good to go.
    Here's how:

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Unbenanntes Dokument</title>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
      </head>
      
      <body>
        <ul>
          <li url="http://www.google.com" id="1">Google</li>
          <li url="http://www.yahoo.com" id="2">Yahoo</li>
          <li url="http://www.bing.com" id="3">Bing</li>
          <li url="http://www.facebook.com" id="4">Facebook</li>
          <li url="http://www.amazon.com" id="5">Amazon</li>
        </ul>
        <script>
          $(document).ready(function() {
            $('li').click(function() {
              alert($(this).attr('url'));
            });
          });
        </script>
      </body>
    </html>

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2012
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Pullo,

    I got is working.


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
  •