2

Is there a more efficient way of storing my checkbox values?

Each checkbox has an assigned value, I was wondering if there was a better way of storing these values (destinationoflink1,2,3,etc)? perhaps store them as an array and call them?...although I am unsure

HTML page extract:

    <form>
        <label for="checkbox1">Link #1</label>
        <input type="checkbox" id="checkbox1" value="http://www.destinationoflink1.com">
        <label for="checkbox2">Link #2</label>
        <input type="checkbox" id="checkbox2" value="http://www.destinationoflink2.com">
        <label for="checkbox3">Link #3</label>
        <input type="checkbox" id="checkbox3" value="http://www.destinationoflink3.com">

        <input type="button" value="open links" id="open_link"/>
    </form>

Javascript file extract (if useful):

$("#open_link").click(function() {
    performOpenLink();
})

function performOpenLink(){
$("input[type=checkbox]").each(function() {
        if (this.checked) {
            window.open(this.value)
        }
    });
}

2 Answers 2

1

You could generate your checkboxes dynamically using an array

Modified HTML

<form>
    <div id="checkbox_container"></div>    
    <input type="button" value="open links" id="open_link"/>
</form>

Modified JavaScript

var destinations = [
    {'label': 'Checkbox1', 'value' : 'http://www.destinationoflink1.com'},
    {'label': 'Checkbox2', 'value' : 'http://www.destinationoflink2.com'},
    {'label': 'Checkbox3', 'value' : 'http://www.destinationoflink3.com'}
];

$(function(){
    // document onReady

    for (var i=0; i<destinations.length; i++){
        // add the label:
        $('#checkbox_container').append('<label for="' + destinations[i].label + '">Link #' + i + '</label>');

        // add the checkbox:
        $('#checkbox_container').append('<input type="checkbox" id="'+ destinations[i].label+'" value="' + destinations[i].value + '" >');
    }
});

$("#open_link").click(function() {
    performOpenLink();
});

function performOpenLink(){
    $("input[type=checkbox]").each(function() {
        if (this.checked) {
            window.open(this.value)
        }
    });
}

This will loop through the array and build the label and checklist html based on properties of each array item. By placing the values into an array, it should be easier for you to add (or remove) new Checklist values later

Here's a jsFiddle


Update: I fixed the issue where I left this in the code. Replaced with destinations[i]

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is along the lines of what I was searching for
0

Here's another approach...

$("#open_link").click(function(e) {
    e.preventDefault();
    $.each($("input[type=checkbox]:checked"), function(index, item){
        window.open( item.value, index );
    });
});

and this is the fiddle

It doesn't open multiple tabs in chrome, but it works perfectly in firefox(v38.0 for me)..

1 Comment

It's an ad block/pop up blocker issue, but that's something else I need to figure out how to fix :S

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.