0

I made this code to select one checkbox when I click on a image but this don't work.

$linkToInterview = 'checkById(document.myform.checkbox,$interview_id)';
?>
<a href="javascript:void(0);" onclick='<?=$linkToInterview?>'><img src="<?=siteConfig::img_dir?>new_photos/video.gif" style="border:0px"/></a>

And Javascript function:

<script type="text/javascript">
function checkByID(chk,id) {
chk[id].checked = true ;
}
</script>

And checkbox code:

<td><input type="checkbox" name="checkbox" value="<?php echo $fet['id']; ?>"

2 Answers 2

1
$linkToInterview = 'checkById(document.myform.checkbox,$interview_id)';

will contain literal $interview_id, which means you're outputting

<a href="..." onclick='checkById(document.myform.checkbox,$interview_id)'>

To contain the content of $interview_id, you should use double quote, add a single quote around it, and use double quote on HTML output:

<?php
$linkToInterview = "checkById(document.myform.checkbox,'$interview_id')";
?>
<a href="..." onclick="<?=$linkToInterview?>;">

This will output (assuming your $interview_id="test"):

<a href="..." onclick="checkById(document.myform.checkbox,'test');">
Sign up to request clarification or add additional context in comments.

Comments

0

Okay let's just assume the problem is your javascript. The way you use JQuery should be similar to the way you use Javascript. The idea of this is whenever you click on the image, by passing the ID of the image --> you get ID of the equivalent checkbox (this depends on how you want to do it) --> trigger the checkByID function to do check/uncheck job for you.

    <script type="text/javascript">
    function checkByID(ID_Of_My_Checkbox) {
    $('ID_Of_My_Checkbox').attr('checked','checked');
     }
    </script>

7 Comments

What ID ? The value of checkbox ?
Your checkbox ID, <td><input type="checkbox" ID="ThisIsAnID" name="checkbox" value="<?php echo $fet['id']; ?>"
Remember to add reference at the top of your html file to use JQuery :D docs.jquery.com/How_jQuery_Works
Yes I know this. I put that code on top of the page right ? When I click on the image I have to send the ID ... how I call JQuery function and send ID has argument ?
Updated with JQuery, don't work also: <script src="google.com/jsapi" type="text/javascript"></script> <script type="text/javascript">google.load("jquery", "1.8.2");</script> <script type="text/javascript"> function checkByID(ID_Of_My_Checkbox) { $('ID_Of_My_Checkbox').attr('checked','checked'); } </script>
|

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.