tags:

views:

262

answers:

4
var subjectList;

function PageMaster()
{
    this.contentDiv = document.getElementById("content");
}

/**
 * Builds the main part of the web page based on the given XML document object
 *
 * @param {Object} xmlDoc   the given XML document object
 */

PageMaster.prototype.doIt = function(xmlDoc)

{
    alert("PageMaster()");

alert("Clear page...");
this.contentDiv.innerHTML = "";

if (null != xmlDoc) 
{
 alert("Build page...");

 //create div Post
 var divPost = document.createElement("div");
 divPost.className = "post";

 //create h1 element
 var h1Element = document.createElement("h1");
 var headingText = document.createTextNode("Invitations");
 h1Element.appendChild(headingText);

 //insert h1 element into div post
 divPost.appendChild(h1Element);

    subjectList = xmlDoc.getElementsByTagName("subject"); 
 var groupList = xmlDoc.getElementsByTagName("group");

 for (var i = 0; i < subjectList.length; i++) //for each subject
 {
  var divEntry = document.createElement("div");
  divEntry.className = "entry";

  var subjectNum = subjectList[i].attributes[0].nodeValue;
  var subjectName = subjectList[i].attributes[1].nodeValue;
  var groupId = groupList[i].attributes[0].nodeValue;
  var groupName = groupList[i].attributes[1].nodeValue;
  var ownerId = groupList[i].attributes[2].nodeValue;

  //set up the invitation table attributes 
  var table = document.createElement("table");
  table.width = 411;
  table.border = 3;
  table.borderColor = "#990000"

  tableRow = table.insertRow(0);
  tableCell = tableRow.insertCell(0);

  var cellContent = "";

  //create invitation message
  var invitationMsg = "<p>Subject : " + subjectNum + " - " + subjectName + "</p>";
  invitationMsg += "<p>You are invited to join " + groupName + " (groupId : " + groupId + ") by owner Id:" + ownerId + "</p>";
  cellContent += invitationMsg;

  //create buttons
  cellContent += "<input type='button' id='acceptButton" + i + "' value='Accept' onclick='acceptInvitation(i)'>"
  cellContent += "<input type='button' id='declineButton" + i + "' value='Decline'>"

  tableCell.innerHTML = cellContent;

  divEntry.appendChild(table);

  var blankSpace = document.createElement("p");
  divEntry.appendChild(blankSpace);
  divPost.appendChild(divEntry); 
 }

 //insert div post into div content
 this.contentDiv.appendChild(divPost);
    }
};

function acceptInvitation(i)
{
    alert("hello");
    //alert(subjectList[i].attributes[0].nodeValue);
}

above is extract of my javascript code. What the code do is to create a table of inviting group from the xml file with accept and decline button. when user press accept, the table will disappear and the table below will move up. For now I am only testing my accept invitation button to see if it works.But my onclick function in the accept button does not work for some reason I don't understand. the alert in acceptInvitation() is not read. Any help will be appreciated. Thanks

A: 

Try to call it like this

onclick='acceptInvitation();'

Not like this

onclick='acceptInvitation(i)'
Gushiken
+2  A: 

What about:

cellContent += "[...] onclick='acceptInvitation("+i+")'>"

This ensures that i is evaluated with the value of the variable instead of as a literal

PatrikAkerstrand
thanks, not realizing that I could do it that way. By the way, I am using inlining method for the onclick event, do you now anyway I could you use eventlistener but still be able to pass the value of "i"so something like.triggerLink=document.getElementById("accept");triggerLink.addEventListener("click",(function name),false);But it seems that there is no way to include the "i"
A: 

while perhaps not addressing the central problem,

onclick='acceptInvitation(i)'

in this case i would be undefined.

onclick='acceptInvitation("+i+")'

would solve at least one problem. Also, you're using an unusual mixture of innerHTML and DOM methods. Why not stick to the DOM methods and use attachEvent/AddEventListener?

edit: A list apart has a good article on binding of variables at http://www.alistapart.com/articles/getoutbindingsituations/

The following is a somewhat specialized example. See the article for more generalized case (or use a library like Prototype)

var click_hdlr = function() {
    return function() {
        return acceptInvitation.apply(this,arguments);
    }
}

var acc_btn = document.createElement("input");
acc_btn.setAttribute("type","button");
acc_btn.setAttribute("id","accept");
acc_btn.setAttribute("value","Accept");
acc_btn.addEventListener("click",click_hdlr(i),false);
Jonathan Fingland
that is what I want to do earlier, well infact I have to use eventListener , but for this case I don't know how to pass the "i".Any idea on how to do that? how could I use eventListener but still be able to pass the "i"
do I put the click_hdlr inside my for loop function?. I am new to javascript and has only learned in for about a week, therefore not quite sure how to do it properly. thanks for the help
Sorry, I should have specified, it doesn't need to be inside the for loop.
Jonathan Fingland
is the "this" = "i"?
I am trying to change the code to suit my program but I don't seems to understand how it works. could you please explain in detail or give me example that works with my code. Thanks so much
apply is a method belonging to all functions. the first parameter is the context. It allows you to apply the method of one object as if it were really the method of another object. The second parameter is an array. In this case I've used the built in `arguments` (an array-like object of the arguments passed to the function). See https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Function/apply for a better explanation
Jonathan Fingland
A: 

dont know if that's what's causing your problem but your outputting onclick='acceptInvitation(i)' Im guessing you want to output acceptInvitation(value-of-i), that is acceptInvitation(" + i + ")

Rune FS