/    Sign up×
Community /Pin to ProfileBookmark

Problems adding dynamic content

Hi all,

I am new to JavaScript in the grand scheme of things. I have been dabbling for about 1 month now. Mainly looking at other peoples code and trying to figure out how it works. I now find myself in need of creating a specific code. I want to create a table that contains the following elements:

a text box (input)
3 select inputs (all 3 using different values)
and a check box.

I want the initial table to be set up with a button under it that allows me to add new lines with the above pre-defined content. I started out small and got it working with text in 1 cell and a select in the other. When I tried to add a text box (input) in the first cell it all went a bit wrong.

Here is my code so far:

[CODE]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<SCRIPT LANGUAGE=”JavaScript”>

function addRow(id){
var tbody = document.getElementById(id)

var row = document.createElement(“TR”)
var td1 = document.createElement(“TD”)

//td1.appendChild(document.createTextNode(“row 2_column 1”))
var textbox = document.createElement(“input”)
textBox.type = “text”
textBox.size = “50”

//td1.appendChild (textBox)

var td2 = document.createElement(“TD”)

//Creating a Select Box
var selectBox = document.createElement(“Select”)
selectBox.name=”mySelectBox”

selectBox.options[selectBox.options.length]=new Option(“Banana”,”1″)
selectBox.options[selectBox.options.length]=new Option(“Melon”,”2″)
selectBox.options[selectBox.options.length]=new Option(“Orange”,”3″)

td1.appendChild (textBox)
td2.appendChild (selectBox)
row.appendChild(td1)
row.appendChild(td2)

tbody.appendChild(row)

}

</script>
</head>

<body>
<a href=”javascript:addRow(‘myTable’)”>Add row</a>

<table id=”myTable” cellspacing=”0″ border=”1″>

<tr>
<td>row1_column1</td><td>row1_column1</td>
</tr>

</table>
</body>
</html>[/CODE]

Please can anyone point out what I have done wrong, as I said I am new to all this so am still learning.

Thanks

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@AbernusauthorApr 24.2013 — Something just clicked and I have got it working with an input and select. Sorry if I wasted anyone's time. Working code:

[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<SCRIPT LANGUAGE="JavaScript">

function addRow(id){

var tbody = document.getElementById(id)

var row = document.createElement("TR")
var td1 = document.createElement("TD")

//td1.appendChild(document.createTextNode("row 2_column 1"))
//Creating the Text Box
var inputBox = document.createElement("input")
inputBox.type = "text"
inputBox.size = "50"

td1.appendChild (inputBox)

var td2 = document.createElement("TD")

//Creating a Select Box
var selectBox = document.createElement("Select")
selectBox.name="mySelectBox"

selectBox.options[selectBox.options.length]=new Option("Banana","1")
selectBox.options[selectBox.options.length]=new Option("Melon","2")
selectBox.options[selectBox.options.length]=new Option("Orange","3")

td2.appendChild (selectBox)
row.appendChild(td1)
row.appendChild(td2)

tbody.appendChild(row)


}

</script>

</head>

<body>
<a href="javascript:addRow('myTable')">Add row</a>

<table id="myTable" cellspacing="0" border="1">

<tr>
<td>row1_column1</td><td>row1_column1</td>
</tr>


</table>

</body>
</html>
[/CODE]


Now to try and add the other 2 selects and the check box. I may be back if I get stuck ?
Copy linkTweet thisAlerts:
@PadonakApr 24.2013 — 
  • 1. they say that it's a good habit to end up script lines with semicolons

  • 2. if you try to investigate what your 'tbody' really is you'll be surprised:


  • <i>
    </i>function addRow(id){ <br/>
    var tbody = document.getElementById(id) <br/>
    [COLOR="#FF0000"]alert(tbody.tagName);[/COLOR]
    var row = document.createElement("TR") <br/>
    var td1 = document.createElement("TD") <br/>
    ×

    Success!

    Help @Abernus spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.4,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...