How to dynamically create select input using JS - javascript

So I am trying to build an app where you can add shifts to a web page. I can add elements and text inputs to the page, however the select inputs are not showing up on the page
What it is supposed to look like
What it actually looks like
As you can see all other elements, besides selects, are working fine...
Code
var newShift = $(
"<div id='Shift" + numOfShifts + "'>" +
"<p class='white-text'>Shift" + numOfShifts + "</p>" +
"<div class='row'>" +
"<div class='col s4 offset-s2'>" +
"<div class='row'>" +
"<p class='white-text'>Shift Starts at:</p>" +
"<div class='input-field col s6'>" +
"<select id='shiftStart'" + numOfShifts + "' class='start-hour front'>" +
"<option value='' disable selected>Hour</option>" +
"<option value='00'>12am</option>" +
"<option value='01'>1am</option>" +
"<option value='02'>2am</option>" +
"<option value='03'>3am</option>" +
"<option value='04'>4am</option>" +
"<option value='05'>5am</option>" +
"<option value='06'>6am</option>" +
"<option value='07'>7am</option>" +
"<option value='08'>8am</option>" +
"<option value='09'>9am</option>" +
"<option value='10'>10am</option>" +
"<option value='11'>11am</option>" +
"<option value='12'>12pm</option>" +
"<option value='13'>1pm</option>" +
"<option value='14'>2pm</option>" +
"<option value='15'>3pm</option>" +
"<option value='16' > 4pm</option >" +
"<option value='17'>5pm</option>" +
"<option value='18'>6pm</option>" +
"<option value='19'>7pm</option>" +
"<option value='20'>8pm</option>" +
"<option value='21'>9pm</option>" +
"<option value='22'>10pm</option>" +
"<option value='23'>11pm</option>" +
"</select>" +
"</div>" +
"<div class='input-field col s6'>" +
"<select class='start-minute front'>" +
"<option value='' disable selected>minutes </option>" +
"<option value='00'>00</option>" +
"<option value='15'>15</option>" +
"<option value='30'>30</option>" +
"<option value='45'>45</option>" +
"</select>" +
"</div>" +
"</div>" +
"</div>" +
"<div class='row'>" +
"<div class='col s4'>" +
"<p class='white-text'>Shift Ends at:</p>" +
"<div class='row'>" +
"<div class='input-field col s6'>" +
"<select class='start-hour front'>" +
"<option value='' disable selected>Hour</option>" +
"<option value='00'>12am</option>" +
"<option value='01'>1am</option>" +
"<option value='02'>2am</option>" +
"<option value='03'>3am</option>" +
"<option value='04'>4am</option>" +
"<option value='05'>5am</option>" +
"<option value='06'>6am</option>" +
"<option value='07'>7am</option>" +
"<option value='08'>8am</option>" +
"<option value='09'>9am</option>" +
"<option value='10'>10am</option>" +
"<option value='11'>11am</option>" +
"<option value='12'>12pm</option>" +
"<option value='13'>1pm</option>" +
"<option value='14'>2pm</option>" +
"<option value='15'>3pm</option>" +
"<option value='16'>4pm</option>" +
"<option value='17'>5pm</option>" +
"<option value='18'>6pm</option>" +
"<option value='19'>7pm</option>" +
"<option value='20'>8pm</option>" +
"<option value='21'>9pm</option>" +
"<option value='22'>10pm</option>" +
"<option value='23'>11pm</option>" +
"</select>" +
"</div>" +
"<div class='input-field col s6'>" +
"<select class='start-minute front'>" +
"<option value='' disable selected>minutes </option>" +
"<option value='00'>00</option>" +
"<option value='15'>15</option>" +
"<option value='30'>30</option>" +
"<option value='45'>45</option>" +
"</select>" +
"</div>" +
"</div>" +
"</div>" +
"</div>" +
"<div class='row'>" +
"<div class='col s8 offset-s2'>" +
"<p class='white-text'>Number Of Employees Needed:</p>" +
"<input id='numOfEmployees' type='text'>" +
"<label for='numOfEmployees'># Of Employees</label>" +
"</div>" +
"</div>" +
"</div>");
$("#add-shift").append(newShift);
Why would I be having trouble adding selects like this??

There's an extra ' after shiftStart in:
"<select id='shiftStart'" + numOfShifts + "' class='start-hour front'>"

Related

How can I get text value from modal with javascript?

I'm fetching the values from my table in ajax.
$(data.mesIzinTanimList).each(function (index, element) {
if (element.izinTanimiVarmi == 'yok')
tr = "<tr class='bg-warning text-warning-50' row='" + element.userId + "' >";
else
tr = "<tr class='bg-light text-white-50 ' row='" + element.userId + "' >";
tr += "<td>" + element.sicilNo + "</td>";
tr += "<td>" + element.adSoyad + "</td>";
tr += "<td>" + element.bagliOlduguKisi + "</td>";
tr += "<td>" + element.bagliOlduguKisiSicilNo + "</td>";
tr += "<td style='display:none'>" + element.bagliOlduguKisiId + "</td>";
tr += "<td> <button onclick= 'tanimlaBtn(" + element.userId + " , " + element.adiSoyadi + " ," + element.bagliOlduguKisiId + ", " + element.bagliOlduguKisi + ")' type='button' class ='btn btn-info'> Tanımla </button></td>";
tr += "</tr>";
$('#IzinTanimListe').append(tr);
});
I open a modal with the define Btn onclick feature.
function tanimlaBtn(userId, adSoyad, bagliOlduguKisiId, bagliOlduguKisi) {
document.getElementById('userId').value = userId;
document.getElementById('bagliOlduguKisiId').value = bagliOlduguKisiId;
document.getElementById('bagliOlduguKisi').value = bagliOlduguKisi;
document.getElementById('adSoyad').value = adSoyad;
}
The adSoyad and bagliOlduguKisi information appears as undefined. They should be string values.
How can I solve this?
The parameters for the function should be wrapped in doubles quotes and escaped e.g \"
Try replacing:
<button onclick= 'tanimlaBtn(" + element.userId + " , " + element.adiSoyadi + " ," + element.bagliOlduguKisiId + ", " + element.bagliOlduguKisi + ")'
With:
<button onclick='tanimlaBtn(" + element.userId + " , " + element.adiSoyadi + " ,\"" + element.bagliOlduguKisiId + "\", \"" + element.bagliOlduguKisi + "\")'

how to check equality between and array and html table value

I check the equality between an array value and html table cell value both are same but how can i check this. I try this but it wont work properly. it always return true if there are same value. check this out.......
function addOrder() {
var bookid = document.getElementById("book_id").value;
var qty = document.getElementById("qty").value;
var price = document.getElementById("unit_price").value;
var total = document.getElementById("dts_total_price").value;
var table = document.getElementById("results");
var tableData = new Array();
$('#results tr').each(function() {
var row = $(this);
tableData.push(row.find("TD").eq(0).html());
});
tableData.shift();
var check;
for (var i = 0; i <= tableData.length; i++) {
var booksId = tableData[i];
alert(bookid);
if (bookid === booksId) {
check = false;
} else {
check = true;
}
}
alert(check);
var table_len = (table.rows.length);
var row = table.insertRow(table_len).outerHTML = "<tr id='row" + table_len + "'>" +
"<td id='book_row" + table_len + "'>" + bookid + "</td>" +
"<td id='qty_row" + table_len + "'>" + qty + "</td>" +
"<td id='price_row" + table_len + "'>" + price + "</td>" +
"<td id='total_row" + table_len + "'>" + total + "</td>" +
"<td><input type='button' id='edit_button" + table_len + "' value='Edit' class='edit' onclick='edit_row(" + table_len + ")'>" +
" <input type='button' id='save_button" + table_len + "' value='Save' class='save' onclick='save_row(" + table_len + ")'> " +
"<input type='button' value='Delete' class='delete' onclick='delete_row(" + table_len + ")'></td></tr>";
document.getElementById("qty").value = "";
document.getElementById("unit_price").value = "";
document.getElementById("dts_total_price").value = "";
};

Append HTML with JS. MVC c#

I add fields dynamically in my View. It's working good, but rules and events don't work for the new field. How do I can apply rules for new fields, like when you load the page for the first time.
If I added a new record in the database and refreshed the page, when rules and validation are working.
$(function addIstPIR() {
var i = #Model.pt3041iResourseBlockedSnyatyZamechaniya.Count-1;
$('.addIstPIR').click(function () {
i++;
var html2Add = "<div class='col-xs-12 col-sm-12 col-md-9 col-lg-6 form-material floating padding-horizontal-3 '>" +
"<select class='form-control' data-val='true' data-val-required='This field is required * !.' id='pt3041iResourseBlockedSnyatyZamechaniya_" + i + "__pt3041iResourseBlockedSnyatyZamechaniya' name='pt3041iResourseBlockedSnyatyZamechaniya[" + i + "].pt3041iResourseBlockedSnyatyZamechaniya'>" +
"<option selected='selected'></option>" +
"<option value='1'>Select 1</option>" +
"<option value='2'>Select 2</option>" +
"<option value='5'>Select 3</option>" +
"<option value='6'>Select 4</option>" +
"</select>" +
"<label class='floating-label text-truncate' for='pt3041iResourseBlockedSnyatyZamechaniya_" + i + "__pt3041iResourseBlockedSnyatyZamechaniya'>Some text</label>" +
"<span class='text-danger field-validation-valid' data-valmsg-for='pt3041iResourseBlockedSnyatyZamechaniya[" + i + "].pt3041iResourseBlockedSnyatyZamechaniya' data-valmsg-replace='true'></span>" +
"</div>" +
"<div class='form-material floating'>" +
"<input id='pt3041iResourseBlockedSnyatyZamechaniya_" + i + "__ppt3041id' name='pt3041iResourseBlockedSnyatyZamechaniya[" + i + "].ppt3041id' value='0' type='hidden'/>" +
"</div>" +
"<div class='col-xs-12 col-sm-9 col-md-7 col-lg-5 form-material floating'>" +
"<input class='form-control tDecimal3' data-val='true' data-val-number='The field Some text must be a number' data-val-required='Some text.' id='pt3041iResourseBlockedSnyatyZamechaniya_" + i + "__pt3041ivolume' name='pt3041iResourseBlockedSnyatyZamechaniya[" + i + "].pt3041ivolume' value='' type='text'>" +
"<label class='floating-label text-truncate' for='pt3041iResourseBlockedSnyatyZamechaniya_" + i + "__pt3041ivolume'>Some Text</label>" +
"</div>";
$('#IFPIRBlock').append(html2Add);
})
})

Creating new fields inside a form with an incremental index in the name attribute, the variable seems to not be considered

This is a jsfiddle about the problem.
http://jsfiddle.net/BHL3P/1/
var FiltersCount = 0;
var currentMinHeight = 20;
var filterDiv = "" +
"<div style='border: 2px solid #003d4c; margin: 5px'>" +
"<div style='width: 45%' class='input select'>" +
"<label for='ReportFilter" + FiltersCount + "Field'>Select a Filter</label>" +
"<select id='ReportFilter" + FiltersCount + "Field' style='width: 100%' name='data[Report][filter][" + FiltersCount + "][field]'>" +
"<option value='product'>Filter" + FiltersCount + "</option>" +
"</select>" +
"</div>" +
"<div style='width: 45%' class='input text'>" +
"<label for='ReportFilter" + FiltersCount + "Value'>Type a Value</label>" +
"<input type='text' id='ReportFilter" + FiltersCount + "Value' style='width: 100%' name='data[Report][filter][" + FiltersCount + "][value]'>" +
"</div>" +
"</div>";
$(document).ready(function () {
$("#moreFilters").append(filterDiv);
$("#addFilter").click(function () {
FiltersCount = FiltersCount + 1;
$("#moreFilters").append(filterDiv);
});
});
I'm creating a series of filters for a research form. Every field have a progressive number that differentiate the name from the others.
I'm clearly doing something wrong because even if the variable is changed, the created HTML keep using the initial value.
try this. i think because you save earlier on variable on top so it wont change value of filterscount inline on variable filterdiv. im not test it but i think it work.. sorry for my english :P
var FiltersCount = -1;
var currentMinHeight = 20;
$(document).ready(function () {
$("#addFilter").click(function () {
FiltersCount = FiltersCount + 1;
var filterDiv = "" +
"<div style='border: 2px solid #003d4c; margin: 5px'>" +
"<div style='width: 45%' class='input select'>" +
"<label for='ReportFilter" + FiltersCount + "Field'>Select a Filter</label>" +
"<select id='ReportFilter" + FiltersCount + "Field' style='width: 100%' name='data[Report][filter][" + FiltersCount + "][field]'>" +
"<option value='product'>Filter" + FiltersCount + "</option>" +
"</select>" +
"</div>" +
"<div style='width: 45%' class='input text'>" +
"<label for='ReportFilter" + FiltersCount + "Value'>Type a Value</label>" +
"<input type='text' id='ReportFilter" + FiltersCount + "Value' style='width: 100%' name='data[Report][filter][" + FiltersCount + "][value]'>" +
"</div>" +
"</div>";
$("#moreFilters").append(filterDiv);
});
});

How to append a value with button's ID in runtime?

I am using below javaScript code to create table in runtime. And i somewhat assumed to append a value into the ID of the button. Will it work. I am confused because of double quotes.
function createTable(){
EmployeeManagement.getList(function(data){
var employee = data;
var x = "<table border= 1 >";
x+="<thead> " + "<tr>" + "<td>" + "Check" + "</td><td>" + "ID" + "</td><td>" + "Name" + "</td><td>" + "Age" + "</td><td>" + "Dept" + "</td><td>" + "Option" + "</td></tr></thead>";
for(i = 0; i<data.length; i++)
{
var id = data[i].id;
x+= "<tr>";
x+= "<td>" + "<input type=checkbox name=Employee value='emp'+id />"
x+= "<td>" + data[i].id + "</td><td>" + data[i].name + "</td>";
x+= "<td>" + data[i].age + "</td><td>" + data[i].dept + "</td>";
x+= "<td>" + "<input id='edit'+id type='button' value='Edit' onclick=edit(this.id) />" + "</td>";
x+= "</tr>";
}
x+= "</table>";
$('#tableHolder').html(x);
});
}
here, i want to append the id value, to the ID value id BUTTON.
I already coded for it. Will it work or how to make it work.
I can use jquery means, how???
Any suggestions would be appreciative...
Thanks
No it won't work. Just do it the same as with the other strings, use string concatenation:
"<input id='edit" + id + "' type='button' value='Edit' onclick=edit(this.id) />"
// --^ --^
Currently, you are generating invalid HTML. But you could have tried yourself to find this out ;)
Yes, this should work. Assuming the id is FOO, the html to generate is:
<input id='editFOO' type='button' ...
which can be done by splitting it up into three parts like so:
"<input id='edit" + id + "' type='button' ..."
Use string concatenation... Also your cells in subsequent rows should match the number of cells in your head row.
function createTable(){
EmployeeManagement.getList(function(data){
var employee = data;
var x = "<table border= 1 >";
x+="<thead> " + "<tr>" + "<td>" + "Check" + "</td><td>" + "Name" + "</td><td>" + "Dept" + "</td><td>" + "Option" + "</td></tr></thead>";
for(i = 0; i<data.length; i++)
{
var id = data[i].id;
x+= "<tr>";
x+= "<td>" + "<input type=checkbox name=Employee value='emp"+id+"' /> </td>";
x+= "<td>" + data[i].id + "</td><td>" + data[i].name + "</td>";
x+= "<td>" + data[i].age + "</td><td>" + data[i].dept + "</td>";
x+= "<td>" + "<input id='edit"+id+"' type='button' value='Edit' onclick=edit(this.id) />" + "</td>";
x+= "</tr>";
}
x+= "</table>";
$('#tableHolder').html(x);
});
}
In JavaScript vars inside a double quoted string are not interpreted. So you have to put the vars outside of the string. Replace e.g. this:
"<input type=checkbox name=Employee value='emp'+id />"
with this:
"<input type=checkbox name=Employee value='emp" + id "' />"

Categories

Resources