0

I am sending an ajax request to load some information constructed in a div from PHP. There is an onClick function there. Ajax responds well, but the JavaScript function does not work in the HTML client.

This is the response from ajax written in PHP:

    $iscId = $row['instructorSemesterCourseId'];
    $year = $row['year'];
    $startDate = $row['startDate'];
    $endDate = $row['endDate'];
    $courseCredit = $row['courseCredit'];
    $pinned = $row['pinned'];
    $semesterName = $row['semesterName'];

    $date1 = date('F j, Y', strtotime($startDate)); 
    $date2 = date('F j, Y', strtotime($endDate));

    $div .= "<div id='SCid".$iscId."' class='schoolClass'>
                  <div class='schoolClassLeft'>
                    <div class='schoolCourseTitle'  onClick='classSessionEnter('SCid".$iscId."'); style='width:100%'>".$semesterName." ".$year." | ".$date1." &ndash; ".$date2."</div>
                  </div>
                </div>";
    echo('$div');

The function written in JavaScript and jQuery is:

function classSessionEnter(elm) {
    alert('elm');
    $('#' + elm).css({"background": "#E7F9CE"});
    $('#' + elm).css({"position": "absolute"});
    $('#' + elm).css({"top": "0px"});
    $('#' + elm).css({"color": "#006600"});
}

The alert does not work - it does not get into the function at all. Why?

3
  • You should scape some single quotes for this to work. Try: onClick='classSessionEnter(\'SCid".$iscId."\');' Commented Jul 5, 2014 at 5:16
  • Yes it is in the global space. It seems the error is coming from the function's variable. When I remove the variable from the function, it will enter into the function. Commented Jul 5, 2014 at 5:18
  • Thanks Michael for editing. :) Commented Jul 5, 2014 at 5:34

2 Answers 2

2

Replace this

$div .= "<div id='SCid".$iscId."' class='schoolClass'>
              <div class='schoolClassLeft'>
                <div class='schoolCourseTitle'  onClick='classSessionEnter('SCid".$iscId."'); style='width:100%'>".$semesterName." ".$year." | ".$date1." &ndash; ".$date2."</div>
              </div>
            </div>";
echo('$div');

with this

$div .= "<div id='SCid".$iscId."' class='schoolClass'>
                  <div class='schoolClassLeft'>
                    <div class='schoolCourseTitle'  onClick='classSessionEnter(SCid".$iscId.");' style='width:100%'>".$semesterName." ".$year." | ".$date1." &ndash; ".$date2."</div>
                  </div>
                </div>";
    echo($div);
Sign up to request clarification or add additional context in comments.

Comments

1
$div .= "<div id='SCid".$iscId."' class='schoolClass'>
    <div class='schoolClassLeft'>
        <div class='schoolCourseTitle' onClick=\"classSessionEnter('SCid".$iscId."')\"; style='width:100%'>".$semesterName." ".$year." | ".$date1." &ndash; ".$date2."</div>
     </div>
</div>";

You should esacpe the quotes first 「onClick=\"」

1 Comment

Thanks a lot. Works great right now. Couple of hours struggling with this.

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.