Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of JavaScript is this?

I have an application that has this format scattered around but I dont know what kind it is. It's not jQuery, so what is it?

$('some_edit').style.display  = "block";
$('some_views').style.display = "none";

I get this in firebug and I know the element is present:

$("some_edit").style is undefined
like image 211
Matt Elhotiby Avatar asked Aug 13 '10 18:08

Matt Elhotiby


2 Answers

It could be many things - examine the source code (or use Firebug) and see what JS libraries are being loaded.

like image 136
Marek Karbarz Avatar answered Oct 12 '22 01:10

Marek Karbarz


A lot of people have defined the '$' symbol as a substitute for document.getElementById().

Basically:

function $(id) { return document.getElementById(id); }
$("ElementID").innerHTML = "Text"; //Usage

A more proper, "namespace" example:

var DOM = { // creating the namespace "DOM"
    $: (function() {
        if(document.getElementById)
            return function(id){ return document.getElementById(id); }
        else if(document.all)
            return function(id) { return document.all[id]; }
        else
            return function(id) { /* I don't even want to get into document.layers */ }
    })()
};

// Later in the code:
{
    function ExampleFunction() {
        // ...
        DOM.$("ElementID").style.backgroundColor = "#96d0a0"; // a nice minty green color
        // ...
    }
}

I have used a self-invocation pattern (function(){ ... }()) in this example.

like image 24
palswim Avatar answered Oct 12 '22 00:10

palswim