1

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]

8
  • 1
    name$? should it be name? Commented Jun 7, 2016 at 10:06
  • first variable is an empty string, [value=] is causing the error..... and not name$ it's valid... attribute ends with selector.. either provide quotes $("[name$=group1][value='" + first + "']") Commented Jun 7, 2016 at 10:06
  • no @guradio i tried using name, but it doesn't work. Looked online somewhere someone used name$ and it worked for me Commented Jun 7, 2016 at 10:07
  • 2
    Note that your HTML is invalid as you have some extra mis-matched double quotes in the span elements Commented Jun 7, 2016 at 10:10
  • 1
    @guradio, name$ is valid attribute selector(attribute starts with) Commented Jun 7, 2016 at 10:10

2 Answers 2

2

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>

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

2 Comments

Thanks fixed it. I'll accept answer as soon as I can
You are suppose to provide reason.. May be attribute value is having space is the reason!
-1

Change this

$("[name$=group1][value=" + first + "]").prop("checked", "true");

to this

$("input[name=group1][value=" + first + "]").prop("checked", "true");

1 Comment

This is logically identical to what the OP already has. Why it was upvoted I have no idea.

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.