0

So here is my problem i have some checkboxes and i want to trigger some behaviour when they are checked or unchecked. So i put a simple javascript function onchange. But i run a test both mozilla and and chrome report a syntax error from a the line where i call my function but i can't find the error. This is so simple it drives me crazy, if u can help me find my error i'd greatly apreciate it. Here is my code :

The php function in which the HTML lines for the checkbox is generated:

 public function addClassToTable(){
 $res='';
 foreach($tab=classes::findAll() as $c){
 $string=$c->__get('niveau').$c->__get("section").$c->__get("division");
 $id=$c->__get('id').$string;
 $res.='<tr><td>'.$string.'</td><td><input type="checkbox" id="'.$id.'" onchange="addClasse('.$id.')"></td></tr>';
 }
 return $res;
 }

The Js function addClasse :

 function addClasse(val){
 var inputElements = document.getElementById(val);
 if(inputElements.checked){
 document.getElementById('selectClasse').style.display="block";
 }
 else{
 document.getElementById('selectClasse').style.display="block";
 }
 }

And here is the Html from which i get the error

 <fieldset>
 <legend>Ajouter une Classe</legend>
 <table>
 <tr>
 <th>Classe</th>
 <th>Selectionner</th>
 </tr>
 <tr><td>12A</td><td><input type="checkbox" id="112A" onchange="addClasse(112A)"></td></tr><tr><td>12A</td><td><input type="checkbox" id="212A" onchange="addClasse(212A)"></td></tr><tr><td>12A</td><td><input type="checkbox" id="312A" onchange="addClasse(312A)"></td></tr>
 </table>
 </fieldset>

The syntax error mention a missing ) after argument list in chrome and identifier starts immediately after numeric literal in mozilla firefox.

yardie
1,5671 gold badge14 silver badges29 bronze badges
asked Dec 30, 2015 at 19:48
5
  • The (112A) has to be in quotes, try: onchange="addClasse('112A')" Commented Dec 30, 2015 at 19:53
  • Add single quote for your parameter. onchange="'addClasse(112A)'" Commented Dec 30, 2015 at 19:53
  • i don't find the id in your html selectClasse. please add it Commented Dec 30, 2015 at 19:56
  • Thanks for quick answer,@antlersoft answer worked fine. i guess i'm tired for not seing this one by myself ! Thanks @Venkatraman, but the js was just a test to see if my function is called, it's ok it work now, Thx to you Commented Dec 30, 2015 at 20:01
  • check my answer for 1 line code snippet and accessing the whole element Commented Dec 30, 2015 at 20:07

4 Answers 4

2

wrap your string like this in onChange Functions argument.

<fieldset>
 <legend>Ajouter une Classe</legend>
 <table>
 <tr>
 <th>Classe</th>
 <th>Selectionner</th>
 </tr>
 <tr>
 <td>12A</td>
 <td><input id="112A" onchange="addClasse('112A')" type=
 "checkbox"></td>
 </tr>
 <tr>
 <td>12A</td>
 <td><input id="212A" onchange="addClasse('212A')" type=
 "checkbox"></td>
 </tr>
 <tr>
 <td>12A</td>
 <td><input id="312A" onchange="addClasse('312A')" type=
 "checkbox"></td>
 </tr>
 </table>
</fieldset>
answered Dec 30, 2015 at 19:54
Sign up to request clarification or add additional context in comments.

Comments

1

There is no need passing id parameter to the addClasse method. Pass this context to access the whole element properties.

Try the below code snippet.

PHP Code: pass the this context

$res.='<tr><td>'.$string.'</td><td><input type="checkbox" id="'.$id.'" onchange="addClasse(this)"></td></tr>';

JS Code

function addClasse(el) {
 document.getElementById('selectClasse').style.display = (el.checked) ? "block" : "none";
}

Note: you missed none string and you can use ternary operator for these kind of conditions.

answered Dec 30, 2015 at 20:06

Comments

0

Try to place the ID in quotes within the javascript function call, like below.

$res.='<tr><td>'.$string.'</td><td><input type="checkbox" id="'.$id.'" onchange="addClasse(\''.$id.'\')"></td></tr>';
answered Dec 30, 2015 at 19:54

Comments

0

addClasse(112A) - the error is because you used letters in an "Integer". You've to make it an "String" using quotes, like: addClasse('112A') (when you call it)

It's correct use \" or \' (if you're using quotes like " in the string) to generate a quote inside the tag attribute (depending if you're doing the attribute with " or ') in the echo string.

answered Dec 30, 2015 at 20:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.