0

This is my current code:

Index.html

    AddEventListener('click', 'a[data-requestpartial]', function (obj, e)
    {
        e.preventDefault();
        // Error is happening here. this is undefined. obj is undefined.
        var url = this.getAttribute('href');
        LoadSection('#container', url, null, function (result)
        {
            window.history.pushState("", "", url);
        });
    });

On Script.JS

function AddEventListener(eventType, query, func)
{
    var objects = Select(query);
    for (var i = 0, len = objects.length; i < len; i++)
    {
        objects[i].addEventListener(eventType, function (e) { func(objects[i], e) });
    }
}

Basically, I am trying to add a "click" event listeners to ALL 'a[data-requestpartial]'. I would like to prevent the anchor default action, and then I would like to do the specific action I've requested.

How can I accomplish this? I've tried both "this" and "obj", however, both of them comes back as undefined.

Edit:

function Select(query)
{
    return query[0] === '#' ? document.getElementById(query.substring(1)) : document.querySelectorAll(query);
}

Here is a JS Fiddle: https://jsfiddle.net/q3Lse37y/2/

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
JohnC1
  • 841
  • 1
  • 12
  • 27

0 Answers0