JavaScript: useful small snippets
There are small and simple JavaScript snippets that can be used to achieve very easy actions. Whenever you need to perform such actions, if you google for help, you will definitely find the answer.
However I want to gather some of those code snippets in one article, just as a future reference for me, and valuable code deposit for you.
So let's see what we got here!
and you want to change its value taking it from a listbox. The magic is done using the OnChange event of the select box with document.getElementById('tobechanged').value=this.options[this.selectedIndex].value. Notice that tobechanged is the name of your input box.
The tag has the onClick trigger:
For example, you might need to perform some actions and then close the window, without confirmation or warnings. In that case you can place the closing command in the tag with onLoad event:
Enjoy.
And please if you have other small and useful snippets, report them in the comments section below!
However I want to gather some of those code snippets in one article, just as a future reference for me, and valuable code deposit for you.
So let's see what we got here!
How to insert a value into a text box, selecting an option from a listbox
The title says it all. Let's imagine you have an input box:
and you want to change its value taking it from a listbox. The magic is done using the OnChange event of the select box with document.getElementById('tobechanged').value=this.options[this.selectedIndex].value. Notice that tobechanged is the name of your input box.
You can achieve the same effect, if you want to do it for more then one field. If you have a table with different values, you can put a button that will trigger a function (in this case called copyme). The onClick event will execute a function whereby it inserts passed values in other input boxes. In the example we populate two elements with id set to "id1" and "id2". The
', '<%=(Recordset.Fields.Item("field2").Value)%>')">
And the function is:
How to clear the content of an element
In order to clear the content of an element we use a simple function, which will set the value of the element to an empty string. Let's create the input box with an id set to "input_box":
We use the onClick trigger to execute the clearme function:
Please notice that '' is two ' without space in between.How to ask for confirmation before performing an action
When performing some actions, you might need to warn the user and ask for confirmation. To do so, in the form we place a little snippet triggered by onSubmit. In the example we have a form that deletes a record and we want to be sure the action is indeed to be taken:
When the user submits the delete request, before performing it, a prompt will pop-up asking for confirmation.How to close a window
If you need to close a window, you can use a small function placed where needed.For example, you might need to perform some actions and then close the window, without confirmation or warnings. In that case you can place the closing command in the tag with onLoad event:
Another method would be to use a form, where the onClick event will trigger the closing of the window:
As a side note remember that you cannot close a window that has not been opened as child. If you copy the script in a page it won't work. If you use a link and open the page from it, it will work. Enjoy.
And please if you have other small and useful snippets, report them in the comments section below!