Reputation: 59
I am currently trying to get this buttons into my HTML table.
This is the code I want to insert:
<button class=\"btn btn-primary btn-xs my-xs-btn\" type='button' onClick='changeRec(".$num.")' >
<span class=\"glyphicon glyphicon-pencil\"></span> Edit
</button>
And here is my attempt at inserting it.
<button type="button" class="btn btn-primary btn-lg" onclick="myFunction()">Add</button>
<script>
function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(-1);
var cellInstruction = row.insertCell(-1);
cellInstruction.innerHTML = '<button class=\"btn btn-primary btn-xs my-xs-btn\" type='button' onClick='changeRec(".$num.")' >
<span class=\"glyphicon glyphicon-pencil\"></span> Edit
</button>';
}
</script>
Upvotes: 0
Views: 15481
Reputation: 15293
You are not concatenating the string correctly and you do not need to escape double quotes.
Here is working example.
<button type="button" class="btn btn-primary btn-lg" onclick="myFunction()">Add</button>
<table id="myTable"></table>
<script>
function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(-1);
var cellInstruction = row.insertCell(-1);
cellInstruction.innerHTML = '<button class="btn btn-primary btn-xs my-xs-btn" type="button" onClick="changeRec(".$num.")" >'
+ '<span class="glyphicon glyphicon-pencil"></span> Edit</button>';
}
</script>
But you should rather separate your javascript from your html and use an eventlistener for the click event.
Here is how you could do that.
var addBtn = document.getElementById('add-btn');
function addEditBtn() {
var table = document.getElementById("myTable");
var row = table.insertRow(-1);
var cellInstruction = row.insertCell(-1);
cellInstruction.innerHTML = '<button class="btn btn-primary btn-xs my-xs-btn" type="button">'
+ '<span class="glyphicon glyphicon-pencil"></span> Edit</button>';
}
addBtn.addEventListener('click', addEditBtn, false);
<button type="button" class="btn btn-primary btn-lg" id="add-btn">Add</button>
<table id="myTable"></table>
An other way would be to create the button element using document.createElement
.
var addBtn = document.getElementById('add-btn');
function addEditBtn() {
var table = document.getElementById("myTable");
var row = table.insertRow(-1);
var cellInstruction = row.insertCell(-1);
var button = document.createElement('button');
var span = document.createElement('span');
button.setAttribute('class', 'btn btn-primary btn-xs my-xs-btn');
button.setAttribute('type', 'button');
span.setAttribute('class', 'glyphicon glyphicon-pencil');
span.innerHTML = " Edit";
button.appendChild(span);
cellInstruction.appendChild(button);
}
addBtn.addEventListener('click', addEditBtn, false);
<button type="button" class="btn btn-primary btn-lg" id="add-btn">Add</button>
<table id="myTable"></table>
Upvotes: 2
Reputation: 10009
I think, you messed somewhere in the brackets. I tested the below code using PHP and it works fine:
<?php
$num = 4;
$insert = "<button class='btn btn-primary btn-xs my-xs-btn' type='button' onClick='changeRec(".$num.")'><span class='glyphicon glyphicon-pencil'></span> Edit</button>";
?>
<!DOCTYPE html>
<html>
<head>
<style>
table, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(-1);
var cell1 = row.insertCell(-1);
cell1.innerHTML = "<?php echo $insert; ?>";
}
</script>
</body>
</html>
function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(-1);
var cell1 = row.insertCell(-1);
cell1.innerHTML = "<button class='btn btn-primary btn-xs my-xs-btn' type='button' onClick='changeRec()' ><span class='glyphicon glyphicon-pencil'></span> Edit</button>";
}
table, td {
border: 1px solid black;
}
<body>
<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>
<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br>
<button onclick="myFunction()">Try it</button>
Upvotes: 0
Reputation: 121
innerHTML only get/set value of an element which doesn't include the label itself
Upvotes: 0