0

So for my code that i have below the reg.onclick is happening before it is even clicked and I'm not really sure how to fix this. Thanks for the help in advance.
function displaySearch(){
var div = document.getElementById('table')
var table = document.getElementById('tableSearch')
var res = xmlhttp.responseXML
var e = res.getElementsByTagName('element')
for(var i = 0; i < e.length; i++){
var splitResult = e[i].textContent.split('#')
var course = splitResult[0]
var num = splitResult[1]
var tut = splitResult[2]
var email = splitResult[3]
var capacity = splitResult[4]
var users = splitResult[5]
var registered = users.split('/')
var rowCount = table.rows.length
var row = table.insertRow(rowCount)
var cell1 = row.insertCell(-1)
cell1.id = "1" + i
cell1.innerHTML = course
var cell2 = row.insertCell(-1)
cell2.id = "2" + i
cell2.innerHTML = num
var cell3 = row.insertCell(-1)
cell3.id = "3" + i
cell3.innerHTML = tut
var cell4 = row.insertCell(-1)
cell4.id = "4" + i
cell4.innerHTML = email
var cell5 = row.insertCell(-1)
cell5.id = "5" + i
cell5.innerHTML = capacity
var cell6 = row.insertCell(-1)
cell6.id = "6" + i
if(users != ""){
for(var i = 0; i < registered.length; i++){
var p = "

" + registered[i] + "

"
cell6.innerHTML = cell6.innerHTML + p
}
}
else{
cell6.innerHTML = ""}
var cell7 = row.insertCell(-1)
cell7.id = "7" + i
var reg = document.createElement('input')
reg.type = 'button'
reg.value = "Register"
reg.id = i
reg.name = i
var createReg = function(arg){registerCourse(arg)}
reg.onclick = createReg(cell7.id)
var cell = "row"+i
cell7.appendChild(reg)
}
}

Edited by mavis310: inline code isn't working

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by Troy III
0

Please remember to put semicolons at the end of your lines in JavaScript.

Also, you are not making a reference to a function, you are executing that function, then making a reference to it's results.

You should have the following:

reg.onclick = function(){...};

instead you are doing the following:

reg.onclick = function(){...}();

the second set of () brackets execute the function, this is what you are doing.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.