Last active
June 14, 2018 14:12
-
-
Save janielMartell/f8d63819b1f4acb90b52e97ecab89ff3 to your computer and use it in GitHub Desktop.
Iterating through DOM elements to filter data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<title>test</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
</head> | |
<body> | |
<select multiple id="select" row="1"> | |
<option>arecibo</option> | |
<option>ponce</option> | |
<option>bayamon</option> | |
<option>guayama</option> | |
</select> | |
<button id="submit">Submit</button> | |
<p class="Campus">arecibo, ponce.</p> | |
<p class="Campus">arecibo, bayamon.</p> | |
<p class="Campus">arecibo, bayamon, ponce, guayama.</p> | |
<script> | |
$('#submit').click(function(){ | |
let arr = $('#select').val(); | |
switch(arr.length) { | |
case 1: | |
console.log("case 1"); | |
checkCampus(arr[0]); | |
break; | |
case 2: | |
console.log("case 2"); | |
checkCampuses(arr[0], arr[1]) | |
break; | |
default: | |
console.log("case default"); | |
break; | |
} | |
}); | |
function checkCampus(substring){ | |
$('.Campus').each(function(){ | |
if(!cond(this, substring)) | |
$(this).hide(); | |
else | |
$(this).show() | |
}); | |
} | |
function checkCampuses(substr1, substr2){ | |
$('.Campus').each(function(){ | |
if(cond(this, substr1) && cond(this, substr2)) | |
$(this).show(); | |
else | |
$(this).hide(); | |
}); | |
} | |
var cond = (elem, str) => $(elem).html().includes(str); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment