1. How to Check if String Contains another Substring
<h2> 1. How to Check if String Contains another Substring?<br> </h2> String 1: <input id="foo" type="text" value="eBay Google Paypal Yahoo"> <br> String 2: <input id="bar" type="text" value="Google"> <br> <br> <button onclick="checkstring()">Click to check if String 1 contains String 2</button> <script> if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(obj, start) { for ( var i = (start || 0), j = this.length; i < j; i++) { if (this[i] === obj) { return i; } } return -1; } } if (!String.prototype.contains) { String.prototype.contains = function(arg) { return !!~this.indexOf(arg); }; } function checkstring() { var foo = document.getElementById("foo").value; var bar = document.getElementById("bar").value; alert(foo.contains(bar)); } </script> <br> <br> <h2>2. How to Remove Array Element by Value?</h2> <br> Array = ["eBay", "Google", "Paypal", "Yahoo" ]; <br> Let's remove element "Google" <br> <br> <button onclick="javsacript:test()">Remove element</button> <script> function removeByValue(arr, val) { for ( var i = 0; i < arr.length; i++) { if (arr[i] == val) { arr.splice(i, 1); break; } } } function test() { var somearray = [ "eBay", "Google", "Paypal", "Yahoo" ]; var old = [ "eBay", "Google", "Paypal", "Yahoo" ]; removeByValue(somearray, "Google"); alert("Array before removing elements: " + old + "\n\nArray after removing elements: " + somearray); } </script>
2. How to Remove Array Element by Value?
<h2>2. How to Remove Array Element by Value?</h2> <br> Array = ["eBay", "Google", "Paypal", "Yahoo" ]; <br> Let's remove element "Google" <br> <br> <button onclick="javsacript:test()">Remove element</button> <script> function removeByValue(arr, val) { for ( var i = 0; i < arr.length; i++) { if (arr[i] == val) { arr.splice(i, 1); break; } } } function test() { var somearray = [ "eBay", "Google", "Paypal", "Yahoo" ]; var old = [ "eBay", "Google", "Paypal", "Yahoo" ]; removeByValue(somearray, "Google"); alert("Array before removing elements: " + old + "\n\nArray after removing elements: " + somearray); } </script>
3. How to Move Listbox Values to Left/Right?
Other must read:
- How to use AJAX, jQuery in Spring Web MVC (.jsp) – Example
- JavaScript to Validate Email & Password Fields on Form Submit Event
<h2> 3. How to Move Listbox Values to Left/Right?<br> </h2> <table> <tbody> <tr> <td><select id="sourceSelect" size="5" multiple=""> <option value="a">eBay</option> <option value="b">Google</option> <option value="c">Paypal</option> <option value="d">Yahoo</option> </select></td> <td> <button onclick="listboxMoveacross('sourceSelect', 'destSelect');">>></button> <br> <button onclick="listboxMoveacross('destSelect', 'sourceSelect');"><<</button> </td> <td><select id="destSelect" size="5" multiple=""> <option value="a">Microsoft</option> <option value="b">Facebook</option> <option value="c">Twitter</option> <option value="d">Pinterest</option> </select></td> </tr> </tbody> </table> <script> function listboxMoveacross(sourceID, destID) { var src = document.getElementById(sourceID); var dest = document.getElementById(destID); for ( var count = 0; count < src.options.length; count++) { if (src.options[count].selected == true) { var option = src.options[count]; var newOption = document.createElement("option"); newOption.value = option.value; newOption.text = option.text; newOption.selected = true; try { dest.add(newOption, null); //Standard src.remove(count, null); } catch (error) { dest.add(newOption); // IE only src.remove(count); } count--; } } } </script>
4. How to Remove Duplicates Elements from Array?
<h2> 4. How to Remove Duplicates Elements from Array? <br> </h2> <pre> var companies = ["eBay", "Google", "Paypal", "Yahoo", "Google"]; </pre> Note 'Google' is duplicate in companies array. Click to remove duplicate elements from companies array:<br> <br> <button onclick="check()">Remove Duplicate</button> <script> function removeDuplicates(arr) { var temp = {}; for ( var i = 0; i < arr.length; i++) temp[arr[i]] = true; var r = []; for ( var k in temp) r.push(k); return r; } function check() { var fruits = [ "eBay", "Google", "Paypal", "Yahoo", "Google" ]; var uniquefruits = removeDuplicates(fruits); alert(uniquefruits); } </script>
5. How to Remove Array Element by Index?
<h2>5. How to Remove Array Element by Index?</h2> <br> Array = ["eBay", "Google", "Paypal", "Yahoo" ]; <br> Let's remove 2nd element, i.e. "Google" <br> <br> <button onclick="javsacript:testRemove()">Remove element</button> <script> function removeByIndex(arr, index) { arr.splice(index, 1); } function testRemove() { var test = [ "eBay", "Google", "Paypal", "Yahoo" ]; var old = [ "eBay", "Google", "Paypal", "Yahoo" ]; removeByIndex(test, 2); alert("Array before removing elements: " + old + "\n\nArray after removing elements: " + test); } </script>
You may be interested in list of all JSON, AJAX, jQuery, Java Tutorials.