So, I'm trying to write a bit of jQuery that pulls the value and custom data (html5) from selected checkboxes to create a list of links. The link text will come from the value while the link source will come from the custom data. I have created arrays for both the link text and urls, but when I try to create a list of links for the selected options, the url shows as being just one letter. Any thoughts about what I'm doing wrong? I've created a JSFiddle here: http://jsfiddle.net/3wx6d5cy/
HTML:
<form method="post" id="resources">
<label><input type="checkbox" data-url="http://option1.com" value="Option 1" >Option 1</label>
<label><input type="checkbox" data-url="https://option2.com" value="Option 2" >Option 2</label>
<label><input type="checkbox" data-url="https://option3.com" value="Option 3" >Option 3</label>
<button type="button" id="showResults">Show Resources</button>
</form>
<ul id="results"></ul>
JS:
$("document").ready(function () {
$("#showResults").click(function () {
var linkValues = $('input:checkbox:checked').map(function () {
return this.value;
}).get();
var linkURL = $('input:checkbox:checked').data('url');
// Check if any checkboxes are selected
var ifChecked = $('#resources :checkbox:checked').length;
function switchCheck(n) {
if (n == 0) {
caseNum = 0;
} else {
caseNum = 1;
}
}
switchCheck(ifChecked);
switch (caseNum) {
// Alert if no checkboxes are selected
case (0):
alert("Please select an option.");
break;
// Store value and data attributes in an array
case (1):
$.each(linkValues, function (i, val) {
$("#results").append("<li><a href='" + linkURL[i] + "'>" + val + "</a> ");
});
break;
}
});
});