I want to apply background to each check box and separated by some space. Is it possible to apply background to dynamically created check box in div
I have to set background to this line.
'<input type="checkbox" value="'+ ar2[i] + " " + " " + ar1[i] + '"/>'
+ ar1[i] + " " + " " + ar2[i] + '</br>' + '</br>' + '</br>'
for ( var i = 0; i < ar2.length; i++) {
var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.after().html('<input type="checkbox" value="'+ ar2[i] + " " + " " + ar1[i] + '"/>' + ar1[i] + " " + " " + ar2[i] + '</br>' + '</br>' + '</br>' );
newTextBoxDiv.appendTo("#TextBoxesGroup");
}
Try;
classx="classname";
for ( var i = 0; i < ar2.length; i++) {
var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.after().html('<input class="'+classx+'" type="checkbox" value="'+ ar2[i] + " " + " " + ar1[i] + '"/>' + ar1[i] + " " + " " + ar2[i] + '</br>' + '</br>' + '</br>' );
newTextBoxDiv.appendTo("#TextBoxesGroup");
}
You may create custom classes in css and assign one class name to classx and it will apply the class to the dynamically created checkbox. The checkbox will get styled accordingly.
Related
I'm facing a freak problem in jquery. When I try to populate a list from a table it becomes empty if it contains a special character like sid'ahmed. When I change it to sidi ahmed it works perfectly. This is my code to populate two lists:
$.getJSON("get-data.php?dat=driver", function(data) {
var stringToAppend1 = "<option value='" + day_Shift + "'>" + day_Shift + "</option>";
var stringToAppend2 = "<option value='" + night_Shift + "'>" + night_Shift + "</option>";
$.each(data, function(key, val) {
stringToAppend1 += "<option value='" + val.prenom + " / " + val.nom + " : " + val.telephone + "'>" + val.prenom + " / " + val.nom + " : " + val.telephone + "</option>";
stringToAppend2 += "<option value='" + val.prenom + " / " + val.nom + " : " + val.telephone + "'>" + val.prenom + " / " + val.nom + " : " + val.telephone + "</option>";
});
$("#night_Shift_text" + id).html(stringToAppend2);
$("#day_Shift_text" + id).html(stringToAppend1);
});
You should escape the single quote by using replace function as shown below. Use this replace whereever needed.
day_Shift = day_Shift.replace(/'/g, "\\'");
Hope this helps!!
Given the following snippet:
out.println("<form action=" + "./post" + " " + "method=" + "post" + " " + "id=" + "tweetForm" + ">");
for (int i = 1; i <= twParser.currentTweetIndex; i++) {
output = twParser.tweetArray[i] + newLine;
out.println("<p>");
out.println("<textarea" + " " + "name=text" + " " + "id=\"styled\"" + " " + "maxlength=140" + " " + "cols=" + "140" + " " + "rows=" + "1" + " " + "tag=" + "text_" + String.valueOf(i) + " " + "form=" + "tweetForm" + " " + "onfocus=\"setbg('#e5fff3');\" onblur=\"setbg('white')\"" + ">" + output + "</textarea>");
out.println("<span class=label-style-countdown" + " " + "id=" + "chars" + String.valueOf(i) + ">" + String.valueOf(140 - twParser.tweetArray[i].length()) + "</span> characters remaining");
out.println("<p>");
}
out.println("<input type=" + "submit" + " " + "name=" + "post" + " " + "value=" + "post" + " " + "style=\"float: left;\"" + "/>");
out.println("<button type=\"reset\" value=\"Reset\">Reset</button>"
...that creates HTML multiple textarea elements and posts them to a servlet. But since all the textareas have the same name, only the contents of the first textarea are posted.
Is there a way to post them all?
Thanks
To have multiple inputs from same name you can use name array like
<textarea name="text[]">You text here</textarea>
which will post all the values having same name as an array.
PS: This can be done with any input types expect radio buttons
On this line:
out.println("<textarea" + " " + "name=text" + " " ...
Append i to the name of the textarea, such that the names increase as text1, text2 etc.
out.println("<textarea" + " " + "name=text" + i.toString() + " " ...
Perform the same loop on the server when receiving the POST request to receive from each textarea.
I receive from the server xhtml code which I want to show in Android app like ebook reader. For this the Monocle library was chosen.
Next I prepare webview with follow code:
contentView.addJavascriptInterface(new LectureJSInterface(), "Android");
contentView.getSettings().setAllowFileAccess(true);
contentView.getSettings().setJavaScriptEnabled(true);
contentView.setBackgroundColor(0xFFECECEC);
contentView.setWebChromeClient(new WebChromeClient());
contentView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Global.dismissProgressDialog();
}
});
After this I download the xhtml code from the server and add to it JS code for working with Monocle:
private String prepareCode(String code) {
if ((code == null) || code.equals("")) return "";
String newCode = code.substring(code.indexOf("<html"), code.indexOf("<head>")+6);
newCode = newCode.concat(
"<script src=\"file:///android_asset/monocore.js\"></script>\n" +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"file:///android_asset/monocore.css\" />\n" +
"<style>\n" +
" #reader {\n" +
" width: 100%;\n" +
" height: 100%;\n" +
" border: 0px solid #000;\n" +
" }\n" +
"</style>\n" +
"<script>\n" +
"\n" +
" var isNightMode = false;\n" +
" var isFirstLoading = false;\n" +
" var startPageNumber = 1;\n" +
"\n" +
" function setSettingsForFirstLoading(fontSize, pageNumber, nightMode) {\n" +
" Android.printLogInfo('setSettingsForFirstLoading()');\n" +
" isFirstLoading = true;\n" +
" isNightMode = nightMode;\n" +
" startPageNumber = pageNumber;\n" +
" window.changeFontSize(fontSize);\n" +
" }\n" +
"\n" +
" function changeFontSize(fontSize) {\n" +
" Android.printLogInfo('changeFontSize()');\n" +
" window.reader.formatting.setFontScale(fontSize);\n" +
" }\n" +
"\n" +
" function nightModeOn() {\n" +
" Android.printLogInfo('nightModeOn()');\n" +
" isNightMode = true;\n" +
" var i = 0;\n" +
" var frame;\n" +
" while (frame = window.reader.dom.find('component', i++)) {\n" +
" frame.contentDocument.body.style.backgroundColor = '#1F1F1F';\n" +
" frame.contentDocument.body.style.color = '#ECECEC';\n" +
" }\n" +
" }\n" +
"\n" +
" function nightModeOff() {\n" +
" Android.printLogInfo('nightModeOff()');\n" +
" isNightMode = false;\n" +
" var i = 0;\n" +
" var frame;\n" +
" while (frame = window.reader.dom.find('component', i++)) {\n" +
" frame.contentDocument.body.style.backgroundColor = '#ECECEC';\n" +
" frame.contentDocument.body.style.color = '#1F1F1F';\n" +
" }\n" +
" }\n" +
"\n" +
" function turnPage(pageNumber) {\n" +
" Android.printLogInfo('turnPage(' + pageNumber + ')');\n" +
" window.reader.moveTo({ page: pageNumber });\n" +
" }\n" +
"\n" +
" function savePercents() {\n" +
" Android.printLogInfo('savePercents()');\n" +
" Android.savePercents(window.reader.getPlace().percentAtTopOfPage());\n" +
" }\n" +
"\n" +
" function moveToPercents(percent) {\n" +
" Android.printLogInfo('moveToPercents(' + percent + ')');\n" +
" turnPage(window.reader.getPlace().pageAtPercentageThrough(percent));\n" +
" }\n" +
"\n" +
" function listenFor(evtName) {\n" +
" Monocle.Events.listen('reader', evtName, report);\n" +
" }\n" +
"\n" +
" function report(evt) {\n" +
" switch (evt.type) {\n" +
" case 'monocle:loaded':\n" +
" Android.calculateSeekBar(window.reader.getPlace().properties.component.lastPageNumber());\n" +
" break;\n" +
"\n" +
" case 'monocle:turn':\n" +
" Android.updatePagesCounter(window.reader.getPlace().pageNumber());\n" +
" break;\n" +
"\n" +
" case 'monocle:recalculated':\n" +
" if (isNightMode) nightModeOn();\n" +
" if (isFirstLoading) { isFirstLoading = false; turnPage(startPageNumber); }\n" +
" Android.calculateSeekBar(window.reader.getPlace().properties.component.lastPageNumber());\n" +
" Android.updatePagesCounter(window.reader.getPlace().pageNumber());\n" +
" break;\n" +
" }\n" +
" }\n" +
"\n" +
" function init() {\n" +
" var options = {\n" +
" flipper: Monocle.Flippers.Slider\n" +
" }\n" +
"\n" +
" listenFor('monocle:turn');\n" +
" listenFor('monocle:loaded');\n" +
" listenFor('monocle:recalculated');\n" +
"\n" +
" window.reader = Monocle.Reader('reader', null, options);\n" +
" }\n" +
"\n" +
" Monocle.Events.listen(window, 'load', init);\n" +
"</script>\n");
newCode = newCode.concat(code.substring(code.indexOf("<head>") + 6, code.indexOf("<body>") + 6));
newCode = newCode.concat("<div id=\"reader\">");
newCode = newCode.concat(code.substring(code.indexOf("<body>") + 6, code.indexOf("</body>")));
newCode = newCode.concat("</div>");
newCode = newCode.concat(code.substring(code.indexOf("</body>")));
return newCode;
}
After finishing this task the WebView loads it:
contentView.loadDataWithBaseURL(baseUrl, prepareCode(code), "text/html", "UTF-8", null);
What I have as results? I tested this app on six devices with Android 4.x and 5.x. On Android 4.x the entire content is shown correctly, but on Android 5.x I see a blank page. I should notice that the xhtml code was loaded because I can call JS-functions and get its results via JS Interface.
Are there any ideas about ways to fixing this bug?
Thanks in advance!
You can try uninstalling the webview system update.
or
Try monocle 2.3.1
or
https://github.com/joseph/Monocle/issues/259
What I'm trying to do is while using a for loop, use the loop counter i to modify values that already have a base number assigned to them in a html element. This is so i can construct multiple hyperlinks with different values. To elaborate here is my html code
var inputs = document.querySelectorAll("input");
var ids = [];
var values = [];
var links = [];
var MasLink = "";
var words = []
for (i = 0; i < inputs.length; i++) {
ids[i] = inputs[i].id;
values[i] = (inputs[i].value * (1 + (0.1 * i)));
console.log(i, values)
};
for (i = 0; i < inputs.length; i++) {
links[i] = "http://data.sparkfun.com/input/public_key?private_key=private_key&altitude=" + values[2] + "&battry_voltage=" + values[3] + "&day=" + values[4] + "&external_temp=" + values[5] + "&heading=" + values[6] + "&internal_temp=" + values[7] + "&latitude=" + values[8] + "&longitude=" + values[9] + "&minute=" + values[10] + "&month=" + values[11] + "&second=" + values[12] + "&speed=" + values[13] + "&year=" + values[14];
words[i] = "<small>" + ids[2] + ":" + values[2] + ", " + ids[3] + ":" + values[3] + ", " + ids[4] + ":" + values[4] + ", " + ids[5] + ":" + values[5] + ", " + ids[6] + ":" + values[6] + ", " + ids[7] + ":" + values[7] + ", " + ids[8] + ":" + values[8] + ", " + ids[9] + ":" + values[9] + ", " + ids[10] + ":" + values[10] + ", " + ids[11] + ":" + values[11] + ", " + ids[12] + ":" + values[12] + ", " + ids[13] + ":" + values[13] + ", " + ids[14] + ":" + values[14] + ", " + ids[15] + ":" + values[15] + ", " + "</small>"
MasLink += "" + words[i] + "<br><br>"
document.getElementById("BuiltLink").innerHTML = MasLink
}
<!DOCTYPE html>
<html>
<body>
<form id="frm1">
<table>
<thead>
<tr>
<td>PublicKey:</td>
<td>PrivateKey:</td>
<td>latitude:</td>
<td>longitude:</td>
<td>altitude:</td>
<td>heading:</td>
<td>speed:</td>
<td>external_temp:</td>
<td>internal_temp:</td>
<td>battry_voltage:</td>
<td>hour:</td>
<td>minute:</td>
<td>second:</td>
<td>year:</td>
<td>month:</td>
<td>day:</td>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" id="PublicKey" value="PublicKey"></td>
<td><input type="text" id="PrivateKey" value="PrivateKey"></td>
<td><input type="text" id="latitude" value="33.505304"></td>
<td><input type="text" id="longitude" value="-86.807809"></td>
<td><input type="text" id="altitude" value="5"></td>
<td><input type="text" id="heading" value="13.7"></td>
<td><input type="text" id="speed" value="3"></td>
<td><input type="text" id="external_temp" value="72"></td>
<td><input type="text" id="internal_temp" value="70"></td>
<td><input type="text" id="battry_voltage" value="12.7"></td>
<td><input type="text" id="hour" value="10"></td>
<td><input type="text" id="minute" value="0"></td>
<td><input type="text" id="second" value="0"></td>
<td><input type="text" id="year" value="2015"></td>
<td><input type="text" id="month" value="6"></td>
<td><input type="text" id="day" value="29"></td>
</tr>
</tbody>
</table>
<button type="button" onclick="BuildLink()" class="btn btn-default">Gimme Link!</button>
</form>
<div>
<a id="printlink" href=""></a>
</div>
<div id="BuiltLink">
</div>
</body>
</html>
Problem is I'm getting the same value each time the code loops. Which shouldn't happen if i is increasing in value for each loop. Please let me know if you need me to explain clearer.
You should persist the value of the i variable in another variable with different scope that your BuildLink function because everytime you call this function all values are reseted.
var increase = 0;
function BuildLink() {
var inputs = document.querySelectorAll("input");
var ids = [];
var values = [];
var links = [];
var MasLink = "";
var words = [];
for (i = 0; i < inputs.length; i++) {
ids[i] = inputs[i].id;
increase = increase + i;
values[i] = (inputs[i].value * (1 + (0.1 * increase)));
console.log(i, values);
};
for (i = 0; i < inputs.length; i++) {
links[i] = "http://data.sparkfun.com/input/public_key?private_key=private_key&altitude=" + values[2] + "&battry_voltage=" + values[3] + "&day=" + values[4] + "&external_temp=" + values[5] + "&heading=" + values[6] + "&internal_temp=" + values[7] + "&latitude=" + values[8] + "&longitude=" + values[9] + "&minute=" + values[10] + "&month=" + values[11] + "&second=" + values[12] + "&speed=" + values[13] + "&year=" + values[14];
words[i] = "<small>" + ids[2] + ":" + values[2] + ", " + ids[3] + ":" + values[3] + ", " + ids[4] + ":" + values[4] + ", " + ids[5] + ":" + values[5] + ", " + ids[6] + ":" + values[6] + ", " + ids[7] + ":" + values[7] + ", " + ids[8] + ":" + values[8] + ", " + ids[9] + ":" + values[9] + ", " + ids[10] + ":" + values[10] + ", " + ids[11] + ":" + values[11] + ", " + ids[12] + ":" + values[12] + ", " + ids[13] + ":" + values[13] + ", " + ids[14] + ":" + values[14] + ", " + ids[15] + ":" + values[15] + ", " + "</small>"
MasLink += "" + words[i] + "<br><br>"
document.getElementById("BuiltLink").innerHTML = MasLink;
}
}
In my opinion you should use Math.random to generate random values instead of just increasing a variable.
After hours of working on this code I figured out my initial thought process was wrong. So I deleted the code and rewrote it. Basically I had to sit down and actually draw a concept on a massive white board. Here is a plunker with the finished code.
http://embed.plnkr.co/HcVfHQ3U5WkLDz1fWfOI/preview
I hope this can help someone else too. Feel free to ask me questions.
I don't understand why my code doesn't work: I try to put a string into input type="text" with jQuery and it doesn't work. It separates different elements of the string when there are space in properties for the input.
My jQuery Code:
<script type="text/javascript">
$(document).ready(function() {
$(".btnEditSerie").click(function (e) {
e.preventDefault();
var tr = $(this).closest('tr');
var $GroupingId = tr.find('#item_GroupingId').val()
localStorage['GroupingId'] = $GroupingId;
var $Title = tr.find('#item_Title').val();
var $Description = tr.find('#item_Description').val();
var $Image = tr.find('#item_Image').val();
$("#b").hide("slow");
$("#btnretour").show();
$('#SerieEdit').append("<label id=" + 'test2' + ">" + "Modification de :" + " " + $Title+ "</label>");
$('#TextEdit').html("<label>" + "Titre :" + " " + "</label>" + "<input type=" + 'text' + " value=" + $Title + " id=" + 'SerieNewName' + " />"
+ "<label>" + "Description :" + " " + "</label>" + "<input type=" + 'text' + " value=" + $Description.toString() + " id=" + 'SerieNewDescription' + "/>"
+ "<label>" + "Image :" + " " + "</label>" + "<input type="+'file'+" value="+$Image.toString()+ " id=" +'SerieNewImage'+"/>");
$("#SerieEdit").show();
$("#TextEdit").show();
})
$('#btnRet').click(function() {
$("#b").show("slow");
$("#SerieEdit").hide();
$("#TextEdit").hide();
$("#SerieEdit").empty();
$("#TextEdit").empty();
$("#btnretour").hide();
})
});
</script>
With val(), for instance $Title must be a string, and when i put $Title in the .Append() it returns a thing like that:
My sad html code:
<h2>EditRefSerie</h2>
<div id="SerieEdit">
<div id="TextEdit">
<label>Titre : </label>
<input id="SerieNewName" type="text" thrones="" of="" value="Games">
<label>Description : </label>
<input id="SerieNewDescription/" type="text" thrones="" of="" value="games">
<label>Image : </label>
<input id="SerieNewImage/" type="file" value="Dessin1.jpg">
</div>
<div id="btnretour" style="">
For the string "Games of thrones" it returns:
<input id="SerieNewName" type="text" thrones="" of="" value="Games">
Have you ideas how to fix it? Do I use jQuery correctly?
You're forgetting quotes:
Your JS:
$('#TextEdit').html("<label>" + "Titre :" + " " + "</label>" + "<input type=" + 'text' + " value=" + $Title + " id=" + 'SerieNewName' + " />"
+ "<label>" + "Description :" + " " + "</label>" + "<input type=" + 'text' + " value=" + $Description.toString() + " id=" + 'SerieNewDescription' + "/>"
+ "<label>" + "Image :" + " " + "</label>" + "<input type="+'file'+" value="+$Image.toString()+ " id=" +'SerieNewImage'+"/>");
Should be:
$('#TextEdit').html("<label>" + "Titre :" + " " + "</label>" + "<input type=" + 'text' + " value='" + $Title + "' id=" + 'SerieNewName' + " />"
+ "<label>" + "Description :" + " " + "</label>" + "<input type=" + 'text' + " value='" + $Description.toString() + "' id=" + 'SerieNewDescription' + "/>"
+ "<label>" + "Image :" + " " + "</label>" + "<input type="+'file'+" value='"+$Image.toString()+ "' id=" +'SerieNewImage'+"/>");
Note the added quotes for the value attributes.
Bonus tip: there's no need to put input types in a concatenated string. It's much more readable if you do:
"<input type='text' value='" + val + "' />"
instead of:
"<input type='" + 'text' + "' value='" + val + "' />"
like you're doing.
You aren't using quotes around your attributes properly e.g.
$('#SerieEdit').append("<label id='" + test2 + "'>" + "Modification de :" + " " + $Title+ "</label>");
ID is unique in HTML page. So you don't need to "find" for retrieve your value.
Also, the variable don't need a "$"
Do something like:
$(document).ready(function () {
$(".btnEditSerie").click(function (e) {
e.preventDefault();
var GroupingId = $('#item_GroupingId').val(); // not need to find if you have a id.. id is unique in the HTML page.
localStorage['GroupingId'] = $GroupingId;
var Title = $('#item_Title').val();
var Description = $('#item_Description').val();
var Image = $('#item_Image').val();
$("#b").hide("slow");
$("#btnretour").show();
$('#SerieEdit').append("<label id='test2'>Modification de : " + Title + "</label>");
$('#TextEdit').html("<label>Titre : </label> <input type='text' value=" + Title + " id='SerieNewName' /> <label> Description : </label><input type='text' value=" + Description + " id='SerieNewDescription'/><label>Image : </label><input type='file' value="+ Image + " id='SerieNewImage'/>");
$("#SerieEdit").show();
$("#TextEdit").show();
});
$('#btnRet').click(function(){
$("#b").show("slow");
$("#SerieEdit").hide();
$("#TextEdit").hide();
$("#SerieEdit").empty();
$("#TextEdit").empty();
$("#btnretour").hide();
})
});