I have this bit of php that works fine as is but I would like to separate out the javascript calls to an onLoad function.

<?php
include("casefile.class");
$cf = new casefile_Rec;
$seclogin=$sess->getSessVar("seclogin");
if ($cf->primary_case_list($seclogin["uname"]))  {
        echo "<ul>";
        while ($row = mysql_fetch_assoc($cf->res)) {

                if ($aLine=="evenListing") { $aLine="oddListing";
                } else { $aLine="evenListing"; }

                printf("<div class=\"%s\">\n", $aLine);

                printf("<li><a onClick=\"wcmsJS.showCaseSummary('%s');\"
                        onDblClick=\"caseWindow('view&cnmbr=%s');\">%s</a>&nbsp;%s&nbsp;%s&nbsp;%s&nbsp;%s</li></div>",
                        $row["casenmbr"], $row["casenmbr"], $row["casecode"], $row["cswcab"], $row["csstamp"], $row["csclaim"], $row["casetyp_code"]);
        }
        echo "</ul>";
} else { echo "You have no Open Cases in the System<br>"; }
?>

This displays an unordered list of records, with alternating colors. Each element of the list has two events, onClick and onDblClick. Because the list is dynamic I have not assigned and id to each discrete <div>. An id is necessary for all event listeners I've seen so my question is what is a good way to create an array of objects and utilize the array in an onLoad (and onUnload) function.

I guess you should use JSON. Perhaps, I misunderstand the question, what is the task?

The task is to separate the display from the data. In this case I'm not entirely convinced it is necessary but I thought worth exploring. I am going to be doing this in a large number of places in one of my current projects. It is widely held adviseable to not encode event handlers in display code, instead set up listeners. In this case each element in an array of records has both on onClick function and an onDblClick function.

I could create an array of strings (JSON perhaps) and save it in a session variable while creating the <ul> or <ol> of records, then let php create JavaScript functions called in onLoad and onUnload. Or, I can leave well enough alone which in this case actually seems easier to maintain. Anyway, I'm just fishing for advise here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.