This chapter explains approximately a way to use arrays to manage DOM elements.
This is the simple approach to address arrays. It iterates all the factors via a listing. You could use this method primarily based at the requirement. As an instance, in case you want to select all of the div factors of a web page, observe the script given underneath. Take a look at the following HTML page which incorporates multiple divs.
You could use the subsequent script to select each individual div from a collection of divs at the web page. The script will select each div and bypass an alert. Take a look at the subsequent script.
You can use the following syntax to handle the above given example. Take a look at the HTML page.
Here, the two divs are enclosed with another div — body_div. While designing a script, we have to select only one external div. Later, by using getElements() method, we can select the two internal divs. Take a look at the following script.
You can use a different method to write the above script as follows. Here, we are using a separate variable to select the body_div.
Whilst manipulating an array of elements, we will pick a particular detail from an array of elements. the following are a few important methods used to manipulate the DOM elements −
This method returns the last element of an array. Let us set up an array to recognize this method.
We can now grab the last element within the array.
The variable lastElement now represents the last element within myArray.
getRandom() method works the similar way like the getLast() method, but will get a random element from array.
The variable randomElement now represents a randomly chosen element within myArray.
MooTools provides a way to copy an array using the $A() function. The following is the syntax for the $A() function.
There are two different methods for adding elements into an array. the first technique lets you add elements one by one or you can merge two different arrays into one.
include () method is used to add an item into an array of DOM elements. for example, consider the following HTML code which contains div elements and one span element under a single and enclosed div — body_div.
In the above code, if we call getElements('div') method on the body_div element, we get one and two div but the span element is not included in the array. If you want to add it into the array you call include () method on the array variable. Take a look at the following script.
Now, the myArray contains both divs and span element.
This technique is used to combine the elements of one array with the elements of another array. This also takes care of the duplicate content. For example, consider the following HTML code which incorporates div elements and two span elements below single and enclosed div — body_div.
In the above code, call getElements('div') method on the body_div element. You get one and div. call $$('.class_name') the method selects the two span elements. You now have one array of div factors and another array of spam factors. If you want to merge those two arrays, then you can use the combined method (). Take a look at the following script.
Now, the myArray contains all the elements of the newArrayToArray variable.
This will help you understand arrays in MooTools. Suppose, we apply the background color to the array of detail which includes divs and span. Check the subsequent code. Here, the second array of elements does no longer belong to any identification or class group and this is why it does not reflect any background color. Take a look at the following code.
You will receive the following output −
MooTools Related Interview Questions
|HTML 5 Interview Questions||Zend Interview Questions|
|MySQL Interview Questions||Drupal Interview Questions|
|UI Developer Interview Questions||CSS Advanced Interview Questions|
|Advanced jQuery Interview Questions||UI Designer Interview Questions|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.