Jump to content

"Dynamically" Create forms


brasuca_e_dai

Recommended Posts

Hello! I'm creating a round-robin tournament generator. - It takes the names, recorded in some "list" array of strings, associating them with an ID number- Generates another array called "games" with the numbers of the teams who will match upand then i need for each game generate a form like the next:<form name="gameK">Player1<input type="text" size="2" name="p1" /> x<input type="text" size="2" name="p2" />Player2<input type="button" name="botao" value="Update" onclick="UpdateScore()" /></form>where the things in bold are variables(something like NameOfPlayer1 |¯¯| x |¯¯| NameOfPlayer2 |Update| )I've tried something about inserting all those HTML on the page, but when the button is clicked, it is said the object doesn't exist.I've heard about something with " .createElement" but i got no clue, even reading tutorials, how it works.Thanks in advance!

Link to comment
Share on other sites

If it says the object doesn't exist when you click on the button then it can't find the UpdateScore function, or something in the UpdateScore function isn't returning a reference to an object that you're trying to reference. The error message should provide some more information that will allow you to determine which case it is.

Link to comment
Share on other sites

EDIT: Just managed what was happening. I'm using IE, and setAttribute doesn't work good for it. Problably it would work on FF or other browser.---------------------I have changed the command to generate elements. Each game is generated this way

function CreateGame(c){  a = jogos[c][0] // this is from where the players are took.  b = jogos[c][1] // each 'lista' element has an object 'cara' where the name goes is   nomea = lista[a].cara + '\t'  nomeb = '\t' + lista[b].cara + '\t'   foo = document.createElement('form')  par = document.createElement('p')  foo.setAttribute('id','jogo'+c)  jog1 = document.createTextNode(nomea)  jog2 = document.createTextNode(nomeb)  vs = document.createTextNode(' x ')  gol1 = document.createElement('input')   gol1.setAttribute('type','text')   gol1.setAttribute('size',2)   gol1.setAttribute('id','pa'+c.toString())  gol2 = document.createElement('input')   gol2.setAttribute('type','text')   gol2.setAttribute('size',2)   gol2.setAttribute('id','pb'+c.toString())  botao = document.createElement('input')   botao.setAttribute('type','button')   botao.setAttribute('value','Atualizar')   botao.setAttribute('id','gerar'+c.toString())/* --------------------------- HERE IS THE PROBLEM	botao.attachEvent('onclick',UpdateScore(c)) ------------------------------------*/  document.body.appendChild(foo)  foo.appendChild(par)  par.appendChild(jog1)  par.appendChild(gol1)  par.appendChild(vs)  par.appendChild(gol2)  par.appendChild(jog2)  par.appendChild(botao)}

I believe the generation is ok because until the commented section is uncommented, all fields and text appear ok.now i wish to add the function UpdateScore© to 'onclick' event of the button that takes the values on the fields generated in the same form. As i created the fields adding the id attribute

gol1 = document.createElement('input')   gol1.setAttribute('type','text')   gol1.setAttribute('size',2)   gol1.setAttribute('id','pa'+c.toString())

i believed that the flollowing lines would do the job

function UpdateScore(c){  a = jogos[c][0]  b = jogos[c][1]  g1 = document.getElementByID('pa'+c.toString()).value  g2 = document.getElementByID('pb'+c.toString()).value// ...}

The code, now corrected is:

[code}function CreateGame©{ a = jogos[c][0] b = jogos[c][1] nomea = lista[a].cara + '\t' nomeb = '\t' + lista.cara + '\t' par = document.createElement('form') jog1 = document.createTextNode(nomea) jog2 = document.createTextNode(nomeb) vs = document.createTextNode(' x ') gol1 = document.createElement('<input type="text" size="2" id="pa'+c.toString()+'" />') gol2 = document.createElement('<input type="text" size="2" id="pb'+c.toString()+'" />') botao = document.createElement('<input type="button" value="Atualizar" id="gerar'+c.toString()+'" onclick="Atualizar('+c+')" />') document.body.appendChild(par) par.appendChild(jog1) par.appendChild(gol1) par.appendChild(vs) par.appendChild(gol2) par.appendChild(jog2) par.appendChild(botao)}[/code]

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...