SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jquery -> normal js conversion

    Here is a piece of javascript code with jquery:
    Code:
    $('li.pages span a').click(function() { whatever(); });
    I need to write same code, but without using jquery. It must be as small as possible and be compatible with modern mobile browsers. What's the most efficient way to replace $('li.pages span a')?

  2. #2
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This should work for you:

    Code JavaScript:
    // jQuery
    $('li.pages span a').click(function() { 
    	whatever(); 
    });
     
    // Plain Javascript
    var list = document.querySelectorAll('li.pages span a'),
    	i = 0;
    for( i; i < list.length; i++ ) {
    	list[i].addEventListener('click', function() {
    		whatever2();
    	}, false)
    }

  3. #3
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The only thing to watch out for is browser support: "querySelectorAll" isn't supported in IE6/7, and "addEventListener" isn't supported in IE6/7/8
    I'm the web overlord for Graphic Business Systems


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
  •