top



I'll be posting some simple js functions here that may or may not be useful. I'm not sure how other people do things, but I'm looking to exploit dynamicness(lol) to the fullest because it would mean less effort for the writer.

Comments, suggestions, criticism, or even your own examples are welcome. Here's 3 functions I felt were good enough to put in my personal "library":

Code:
function clearNhide(t, n) {
    for (x = 0; x <= n; x++) {
        var i = '';
        i = t + x;
        document.getElementById(i).innerHTML = '';
        document.getElementById(i).style.display = 'none';
    }
}

function unHide(t, n) {
    for (x = 0; x <= n; x++) {
        var i = '';
        i = t + x;
        document.getElementById(i).style.display = '';
        http://document.getElementById(i).style.display = 'inline';
    }
}
The purpose was simple. I had a bajillion numbered options for some select boxes and clearing each's contents and hiding them and showing the ones I needed one by one just isn't my thing. Still. These became useless quickly when I realized that it would probably be better to create these options on demand(I wrote something real quick in C++ to make all the empty options for me, so don't worry. I didn't spent hours numbering them XD). So then this came into existence:

Code:

function makeSlct(t, n, where) {
    var newSlct = document.createElement('select');
    newSlct.setAttribute('id', 'slct' + t );
    newSlct.setAttribute('name', 'slct' + t );
    document.getElementById(where).appendChild(newSlct);
    for (x = 0; x <= n; x++) {
        var newOpt = document.createElement('option');
        newOpt.setAttribute('value', x)
        newOpt.setAttribute('id', 'opt' + t + x);
        document.getElementById('slct' + t).appendChild(newOpt);
    }

}
This one isn't as "universal" as the last 2, but it creates a selectbox(named 'slct' + t) and some number of options(named 'opt' + t +(0->n)) and it puts this select box at the end of some element. where is that element's id.

I'm still working on a destroy function and I'm also thinking about how I would make this select box call another function...I found a binaryRead.js so the asp.net got thrown in the recycle bin XD. I'll be using that binaryRead.js to fill in my options.
Code:

function removeSlct(Slctid) {
    if (document.getElementById(Slctid)) {
        var SlctBox = document.getElementById(Slctid);
        while (SlctBox.length > 0) {
            SlctBox.options.remove(0);
        }
        SlctBox.parentNode.removeChild(SlctBox);
    }
}
This is very nice. You just pass the selectbox you want to destroy, and this destroys all of it's options.(idk what happens if you try to destroy a selectbox without destroying it's options.)

And even better is this:

Code:

function removeNextSibling(elementID) {
    var ele = document.getElementById(elementID);
    while (ele.nextSibling) { //while there is a next sibling...
        ele.parentNode.removeChild(ele.nextSibling); //...delete it
    }
}
This one came first:
Code:
function removeElement(elementID) {
    if (document.getElementById(elementID)) {
        var ele = document.getElementById(elementID);
        ele.parentNode.removeChild(ele);
    }
}
removeElement() is real simple. It just removes 1 element. You just gotta pass it an id.
removeNextSibling() removes all the next elements in that tag. A 'right clear', I guess. You just pass the id of the element you want it to stop at. I made this because the page I'm making will have some selectboxes for the user to select from and I want the selectboxes to be able to remove all the next ones if the option is changed. Because most of the options will affect what options appear next. I put them all in a div tag to prevent them from deleting anything else.

This should be posted in the tutorial section.
Moved to Tutorials

o.O. Well I'm honored to have this moved in tutorials. But I also want other people to post some nice functions too. I'm still learning javascript myself so my functions might not be useful to some. But to me they're awesome XD.

Added removeNextSibling().

Well it's actually like a tutorial cause you're sharing your knowledge on this.
Personally, I'm not very well versed with javascript and I'm new with jQuery. And I'm currently lacking the time to learn by myself.
We'll have to see what Unknown Data says. Smile