StabDev StabDev - 5 months ago 19
jQuery Question

Multiple Attribute Selector checkbox

So I've got these labels with input checkboxes in it. Now after submit and the form is not accepted it returns here and I want it to select the previous selected box.

<label class="choice" data-id="1"><input type="checkbox" name="group1" value="use your apple1">use your apple<span class="left" ></span></label>
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="Zo maak je van onbekenden klanten1">Zo maak je van onbekenden klanten<span class="left" ></span></label>
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="Multi media feest">Multi media feest<span class="left" ></span></label>

<label class="choice" data-id="4"><input type="checkbox" name="group4" value="Pretwerk OPTIE!">Pretwerk OPTIE!<span class="left" "></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="use your apple4">use your apple<span class="left" "></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="Snelheid is geld4">Snelheid is geld<span class="left" "></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="Fotograferen met je iPhone">Fotograferen met je iPhone<span class="left" "></span></label>


I've already used a hidden field etc. So I've got the returned value of the previously input.

var first = "<?php echo $first ?>";
//in this case I make first the value 'use your apple1'
$("[name$=group1][value=" + first + "]").prop("checked", "true");


So now it should check the box use your apple in group1
But i get the error

jquery.min.js:2 Uncaught Error: Syntax error, unrecognized expression: [name$=group1][value=use your apple1]

Answer

Add the Single quotes (') value=" + first + "]" to value='" + first + "']"

var first = "<?php echo $first ?>";
//in this case I make first the value 'use your apple1'
$("[name$=group1][value='" + first + "']").prop("checked", "true");

$(document).ready(function(){
		var first = "use your apple1";
//in this case I make first the value 'use your apple1'
$("[name$=group1][value='" + first + "']").prop("checked", "true");
		
		//console.log(str);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="use your apple1">use your apple<span class="left" ></span></label>
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="Zo maak je van onbekenden klanten1">Zo maak je van onbekenden klanten<span class="left" ></span></label>
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="Multi media feest">Multi media feest<span class="left" ></span></label>

<label class="choice" data-id="4"><input type="checkbox" name="group4" value="Pretwerk OPTIE!">Pretwerk OPTIE!<span class="left" "></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="use your apple4">use your apple<span class="left" "></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="Snelheid is geld4">Snelheid is geld<span class="left" "></span></label>
<label class="choice" data-id="4"><input type="checkbox" name="group4" value="Fotograferen met je iPhone">Fotograferen met je iPhone<span class="left" "></span></label>