Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7979777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:51:57+00:00 2026-06-04T09:51:57+00:00

I don’t need to write this in jQuery but I’m not versed enough in

  • 0

I don’t need to write this in jQuery but I’m not versed enough in plain javascript to figure it out. Chris Coyier wrote a nice explanation of what I’m talking about here.

The reason I want to convert it is because I don’t need to include an entire jQuery library for this one piece of code. I can save that extra request by using plain old javascript.

This is the example code that I want to convert:

$(document).ready(function() {
    $(".featured").click(function(){
        window.location=$(this).find("a").attr("href"); return false;
    });
});

Here’s what I’ve come up with so far:

document.addEventListener("DOMContentLoaded", function() {
    document.querySelectorAll("div.feature").click(function(){
        window.location=$(this).find("a").setAttribute("href"); 
        return false;
    });
});

One thing which isn’t correct in this, as far as I know, are the querySelectorAll, which is looking for just a div element, right? The other thing is the $(this), which I don’t know how to translate into plain javascript.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:51:59+00:00Added an answer on June 4, 2026 at 9:51 am

    You can select with this.querySelectorAll(…):

    IE8:

    window.onload = function() {
        // get all dom elements with class "feature"
        var aFeatures = document.querySelectorAll(".feature");
        // for each selected element
        for (var i = 0; i < aFeatures.length; i++) {
            // add click handler
            aFeatures[i].onclick = function() {
                // get href of first anchor in element and change location
                window.location = this.querySelectorAll("a")[0].href;
                return false;
            };
        }
    };
    

    IE9 and other current browser:

    document.addEventListener("DOMContentLoaded", function() {
        // get all dom elements with class "feature"
        var aFeatures = document.querySelectorAll(".feature");
        // for each selected element
        for (var i = 0; i < aFeatures.length; i++) {
            // add click handler
            aFeatures[i].addEventListener('click', function() {
                // get href of first anchor in element and change location
                window.location = this.querySelectorAll("a")[0].href;
                return false;
            });
        }
    });
    

    === UPDATE ===

    For IE7 support you should add following (untested) script before (also see here):

    (function(d){d=document,a=d.styleSheets[0]||d.createStyleSheet();d.querySelectorAll=function(e){a.addRule(e,'f:b');for(var l=d.all,b=0,c=[],f=l.length;b<f;b++)l[b].currentStyle.f&&c.push(l[b]);a.removeRule(0);return c}})()
    

    It is possible that it only supports document.querySelectorAll not element.querySelectorAll.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know if this question is trivial or not. But after a couple
Don't ask me why but i can't use this method because I need to
Don't need to do this right now but thinking about the future... What would
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
Don't ask me why but I need to do the following: string ClassName =
I don't know whether this is really possible, but I'm trying my best. If
I don't think this is particularly quirky, but in an attempt to control my
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this is the right place to ask this, but I will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.