1

I have a set of input checkboxes on a form that look like this:

<input type="checkbox" value=""><span value="Atlanta">Atlanta</span>
<input type="checkbox" value=""><span value="Charleston">Charleston</span>
<input type="checkbox" value=""><span value="Chicago">Chicago</span> and etc.

The above is due to running this, $dataTable.ajax.reload(), and for some reason this erases the values of the checkboxes in my form. These checkboxes are not hardcoded and are being dynamically generating.

Instead of doing the below, I realize I can just refresh the page and get all the value back, but I'd like to see if there is another way to do what I'm trying to do below.

Anywho, I took the span text of each checkbox, turned that into an array and called it spanarr. I also turned the input checkboxes into an array, called emergcheck.

var emergspanarr = $('#emergencyForm span').toArray();
var spanarr = [];
var emergcheck = $('#emergencyForm input[type="checkbox"]');

emergspanarr.map(function(item){
    spanarr.push(item.innerHTML.trim());
});

I'm trying to insert one span value (from spanarr) into each of input checkbox (from emergcheck).

What I have so far:

for (var j = 0; j < emergcheck.length; j++){
    for (var k = 0; k < spanarr.length; k++){
        emergcheck.attr("value", function(i, val){
            return val + spanarr[k];
        })          
    }                   
}

But it's producing this:

<input value="AtlantaCharlestonChicagoHouston (Sales)Houston (Terminal)Long BeachMarseillesMiamiMontreal....Santa Ana SavannahSeattleSecaucusTorontoVancouverAtlantaCharlestonChicagoHouston (Sales)Houston (Terminal)Long BeachMarseillesMiamiMontrealSanta Ana .....Santa AnaSavannahSeattleSecaucusTorontoVancouver" type="checkbox"><span value="Atlanta">Atlanta</span>

<input value="AtlantaCharlestonChicagoHouston (Sales)Houston (Terminal)Long BeachMarseillesMiamiMontreal....Santa Ana SavannahSeattleSecaucusTorontoVancouverAtlantaCharlestonChicagoHouston (Sales)Houston (Terminal)Long BeachMarseillesMiamiMontrealSanta Ana .....Santa AnaSavannahSeattleSecaucusTorontoVancouver" type="checkbox"><span value="Charleston">Charleston</span> and etc.

I want it to be this instead:

<input type="checkbox" value="Atlanta"><span value="Atlanta">Atlanta</span>
<input type="checkbox" value="Charleston"><span value="Charleston">Charleston</span>
<input type="checkbox" value="Chicago"><span value="Chicago">Chicago</span

I feel like I am close. What am I missing? In need of another pair of eyes. Thanks,

4
  • Please include code as code, not as an image. You should be able to copy and paste from the Elements panel (or right-click and select Copy > Copy outerHTML). Commented Sep 11, 2018 at 15:32
  • FYI, map returns an array; there's no need to push. var spanarr = emergspanarr.map(function (item) { return item.innerHTML.trim(); }). Commented Sep 11, 2018 at 15:34
  • @HereticMonkey after copying the outerHTML, I realized the loop was worse than I originally thought (lol, outputs the array 17 times within value ) so I shortened the output for post. Duly noted on the push, I'll change that in my code. Thanks! Commented Sep 11, 2018 at 15:57
  • Try my answer @hnewbie Commented Sep 11, 2018 at 16:37

2 Answers 2

2

Just try to change code with below code -

JS Code -

for (var j = 0; j < emergcheck.length; j++){
    emergcheck.eq(j).val(spanarr[j]);                
}

Maybe it can help you.

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

3 Comments

Could you explain what is happening here? It worked! I've never seen eq() used before. Looked it up @ the jquery site. Was it because my indexes for both arrays needed to be filtered?
It just used to select tags with their index value you can read it more from here .eq().
Thank you for accepting my answer as well for upvote it. :)
1

I think you just want the value of the input. You can use .val() from jQuery or just .value as pure js.

jQuery way:

var emergspanarr = $('#emergencyForm span').toArray();
var emergcheck = $('#emergencyForm input[type="checkbox"]').toArray();

for (var j = 0; j < emergcheck.length; j++) {
    emergcheck[j].val(emergspanarr[j].val());
}

Pure js way:

var emergspanarr = $('#emergencyForm span').toArray();
var emergcheck = $('#emergencyForm input[type="checkbox"]').toArray();

for (var j = 0; j < emergcheck.length; j++){
    emergcheck[j].value = emergspanarr[j].value;
}

3 Comments

Thanks for the response, but neither strangely works. I'll keep digging.
@hnewbie I didn't realize your spanarr held innerHTML. This would only work with an array of elements. My bad. I'll edit my answer to use emergspanarr instead. That should work
The "Pure js way" uses jQuery too (for selecting the elements). If you want pure JS for that, use var emergspanarr = Array.from(document.querySelectorAll('#emergencyForm span')); and similar code for the other one.

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.