I am trying to create a 2 column layout in my JavaScript code.
This is the CSS that I need to apply to my JS code:
div#theLeft {
clear:both;
width:15%;
float:left;
}
div#theRight {
width:83%;
float:right;
padding-bottom:20px;
padding-right:30px;
}
How do I apply this to my JavaScript elements in my .js file?
myText = ("
<html>\n<head>\n<title>Resume</title>\n</head>\n<body>\n");
myText += (userName+"<br>");
myText += (address+ " / " +phoneNo +"<br>");
myText += ("<hr width 70%>");
myText += ("PERSONAL\n DETAILS<br>");
myText += ( +personalData+ "<br>");
myText += ("CAREER\n OBJECTIVES<br>");
myText += ( +careerObj+"<br>");
myText += ("EDUCATION<br>");
myText += ( +edubackgrnd+ "<br>");
myText += ("EMPLOYMENT\n EXPERIENCE<br>");
myText += (fromDate+ " - " +toDate+"<br>");
myText += (experience +"<br>");
myText += (fromDate1+ " - " +toDate1+"<br>");
myText += (experience1 +"<br>");
myText += (fromDate2+ " - " +toDate2+"<br>");
myText += (experience2 +"<br>");
myText += (fromDate3+ " - " +toDate3+"<br>");
myText += (experience3 +"<br>");
myText += ("CHARACTER \n REFERENCES<br>");
myText += ("Upon request<br>");
myText += ("BUSINESS \n REFERENCE<br>");
myText += (busRef);
myText += ("</body>\n</html>");
flyWindow = window.open('about:blank','myPop','width=1000,height=500,left=200,top=200');
flyWindow.document.write(myText);
Here is my JS. If I am understanding correctly, I can add html tags in JavaScript between quotes (""); correct?
In order for that CSS to be applied to a particular HTML file it needs to be included between <style></style> tags somewhere. As an alternative (and much better practice) your CSS should be stored in a separate .css file and included using a <link> tag like so:
<link rel="stylesheet" type="text/css" href='static/style.css'>
As a further alternative, if what you meant by including JS in the question was that you want to apply that style dynamically, like when an event takes place on your page, you can do that by using jQuery. There are certainly native ways to do it but, for this sort of thing, I still prefer jQuery.
$("div#theRight")
.css("width", "83%")
.css("padding", "float:right")
.css("padding-bottom", "20px")
.css("padding-right", "30px");
Finally, the best way would be to include a .css file as mentioned above that applies you styles to elements of a certain class. Then, when your event takes place, add or remove that class from your element(s) to apply or remove those styles dynamically.
$("div#theRight").addClass("myClass");
$("div#theRight").removeClass("myClass");
Hope this helps.
Related
I am reading data from a javascript file and then importing them into the HTML file. The issue I am having is that I am unable to get my p tags and h3 tags in my section tag. This is what it looks like when I load it up:
The following code is what I have tried. I have used inner HTML to create the tags and I am not sure what I can do to fix this.
// create a box class section for whole thing
containerElement.innerHTML += "<section class='flight-info'> ";
// another class for top part of box
containerElement.innerHTML += "<div class='topBox' ";
// add all the elements such as destination and code
containerElement.innerHTML += "<h3 class='destination'> " + FlightInfo[i].destination + " </h3>";
containerElement.innerHTML += "<p class='departTime'> Depart time: " + FlightInfo[i].departTime + " </p>";
// have to check for # of stops to format. if its not 0, we can add the # of stops and time, otherwise its Non-stop, time
if (FlightInfo[i].stops != 0) {
containerElement.innerHTML += "<p class='timeStop'>" + FlightInfo[i].stops + ", " + FlightInfo[i].time + " min </p>";
} else if (FlightInfo[i].stops == 0) {
containerElement.innerHTML += "<p class='timeStop'> Non-Stop" + ", " + FlightInfo[i].time + " min </p>";
}
// end the topBox class
containerElement.innerHTML += "</div>"
containerElement.innerHTML += "</section>"
It's because you keep adding everything to the container.
containerElement.innerHTML +=
You only need the code above one time (at least the way you're doing it now), and add everything in one long string.
Otherwise, add the section, then select the section and add everything to the section's innerHTML. Same idea with the div.
I have a problem concerning multiple file uploads in javascript. I am trying to create my own multiple file upload by dynamically adding inputs. This is all easy as pie, but the problem is that whenever I add a new , my previous input-fields of the type "file" get reset.
If I remove the last lines of code where I alter the innerHTML of my parent div, the values of my do not get reset. Does anyone know how this problem can be solved? The javascript code can be found below. Thanks in advance.
if(document.getElementById("upload_queue").innerHTML.indexOf(_item) == -1)
{
var _row = "<tr id='queue_row_" + items_in_queue + "'>";
_row += "<td>";
_row += "<div class='remove_uploaded_image' onclick='remove_from_queue(" + items_in_queue + ")'></div>";
_row += "</td>";
_row += "<td>";
_row += _item;
_row += "</td>";
_row += "</tr>";
document.getElementById("upload_queue").innerHTML += _row;
document.getElementById("upload_image_" + items_in_queue).style.display = "none";
items_in_queue++;
document.getElementById("uploader_holder").innerHTML +=
'<input id="upload_image_' + items_in_queue +
'" name="upload_image_' + items_in_queue + '" accept="image/jpeg" type="file"' +
'onchange="add_to_upload_queue()" style="display: inline;" />';
}
Yeah... you're going to want to use appendChild instead of modifying the inner HTML:
var myInput = document.createElement("INPUT");
// do stuff to my input
var myContainer = document.getElementById("uploader_holder");
myContainer.appendChild(myInput);
That's the general gist of what you have to do - let me know if you need somethign more specific, but it looks like you've got a good hold on JS already... You're going to want to do that in almost all cases rather than setting inner HTML... So, building your TR as well... you'll have to append the TD to the TR, you'll have to append the TD with your input, you'll have to append your targeted table with the TR, etc.
text += "<button id=next onclick=calendar(nextDate)>"
text += "<button id=next onclick=calendar(prevDate)>"
This code does not seem to run normally.
I do not know what the problem is.
function calendar(date) {
...........
text += "<tr>"
text += "<td colspan=7>"
text += "<button id=prev onclick=calendar(prevDate)>"
text += "◀"
text += "</button>"
text += "<button id=next onclick=calendar(nextDate)>"
text += "▶"
text += "</button>"
text += "</tr>"
text += "</td>"
text += "</table>";
document.getElementById("cal").innerHTML = text;
}
calendar();
Assuming you have set the onClick function to this function, you are missing semicolons at the ends of your lines ";"
Problems
text += "<button id=prev onclick=calendar(prevDate)>"
text += "<button id=next onclick=calendar(nextDate)>"
You do not use quotes to surround the values of the attributes
You pass the strings prevDate and nextDate instead of their values, but since they don't have any quotes surrounding them, javascript will try to find these two variables, but it won't be able to (unless they're in the global scope)
You don not use any semicolons (;), this probably won't cause a problem but it's still a good idea to use them
Here are a couple of ways that allow you to properly pass the previous and next date:
String Concatenation
Interpolation
Event Listeners
String Concatenation
text += "<button id='prev' onclick='calendar(" + prevDate + ")'>";
text += "<button id='next' onclick='calendar(" + nextDate + ")'>";
Interpolation (ES6)
text += `<button id="prev" onclick="calendar('${prevDate}')">`;
text += `<button id="next" onclick="calendar('${nextDate}')">`;
Event Listeners
text += "<button id='prev'>";
text += "<button id='next'>";
// This goes after document.getElementById("cal").innerHTML = text;
document.getElementById("prev").addEventListener("click", function (event) {
calendar(prevDate);
});
document.getElementById("next").addEventListener("click", function (event) {
calendar(nextDate);
});
I have to show dynamically checkboxes in a HTML page and get their value from database.
The trick I used is to create the whole table HTML in java and then using AJAX I do this
var div = document.getElementByID("div").innerHTML = htmlCode;
but issue is
in htmlCode variable, the html is like
<table width="100%">
<tr>
<td width="50%">......
but when I check div.innerHTML it shows like
<TABLE width="100%">
<TBODY>
<TR>
<TD width="50%">
<?xml:namespace prefix = ....
Why they become uppercase and why is xml:namaesapce prefix added ?
This causes issue as table is not properly displayed.
Is there any other better way to do this without using innerHTML ?
The code for generation of table is
String html = "<table width=\"100%\">";
for (Iterator it = lookupList.iterator (); it.hasNext ();)
{
HashTree lookupElement = (HashTree) it.next ();
String code = lookupElement.getChildTagValue ("CODE");
String text = lookupElement.getChildTagValue ("TEXT");
String labelText = "";
if(indexLookupElement == 0)
labelText = "First Checkbox label";
html = html + "<tr><td width=\"50%\" >" +
+
"<input type=\"checkbox\" id=\"item" + code + "\" />" +
"<html:label " +
"id=\"_Description\"" +
"name=\"_Description\"" +
">" + text +
"</html:label>" +
"<td width=\"50%\" >" +
"</td></tr>";
indexLookupElement += 1;
}
html = html + "</table>";
Thanks,
Aiden
It's not a best practice to create the HTML code server-side. If you are sure you'll have the table in your document it's better to make it part of the structure of the document and feed only the variables from your Java code. Even if the table is conditional you may consider hide/unhide it with a variable. I believe this approach will solve your problem if the HTML code of your webpage is correct.
Update
Test it like this:
html = html + "<tr><td width=\"50%\"><input type=\"checkbox\" id=\"item" + code + "\" /><label id=\"_Description\" name=\"_Description\">" + text + "</label></td><td width=\"50%\"></td></tr>";
In your code you have missing TD closing tag and I made some changes. See if this will fix your problem.
Hello friends here's my code
function dropItems(idOfDraggedItem,targetId,x,y)
{
var html = document.getElementById('dropContent').innerHTML;
if(html.length<=0){
html.innerHTML='<img src="images/drag.jpg" alt=" " />'
}
if(html.length>0)html = html + '<br>';
html = html + document.getElementById(idOfDraggedItem).innerHTML;
document.getElementById('dropContent').innerHTML = html;
}
I want to to show a image in div when its empty.. but when it receive any data i want to remove that image by using this script .but its not working plz help me
function dropItems(idOfDraggedItem,targetId,x,y)
{
html = document.getElementById('dropContent');
if(html.innerHTML.length<=0){
html.innerHTML='<img src="images/drag.jpg" alt=" " />'
}
if(html.innerHTML.length>0)html = html + '<br>';
html = html + document.getElementById(idOfDraggedItem).innerHTML;
document.getElementById('dropContent').innerHTML = html;
}
function dropItems(idOfDraggedItem,targetId,x,y)
{
var html = document.getElementById('dropContent');
if(html.innerHTML.length<=0){
html.innerHTML='<img src="images/drag.jpg" alt=" " />'
}
if(html.innerHTML.length>0)html+='<br>';
html += document.getElementById(idOfDraggedItem).innerHTML;
document.getElementById('dropContent').innerHTML = html;
}
var html = document.getElementById('dropContent').innerHTML;
Here your passing the value to 'html'. not the reference of 'dropContent'. So you can't call 'innerHTML' on variables.
//Modified code
function dropItems(idOfDraggedItem,targetId,x,y)
{
var html = document.getElementById('dropContent').innerHTML;
if(html.length<=0){
html = '<img src="images/drag.jpg" alt=" " />'
}
if(html.length>0)html = html + '<br>';
html = html + document.getElementById(idOfDraggedItem).innerHTML;
document.getElementById('dropContent').innerHTML = html;
}
The problem is that the dropContent may not be empty even if it shows nothing; because, Javascript does not ignore the White-Spacing so there may be a white-space that keep the length bigger than zero.
Here is the solution, using the jQuery library as it has the needed methods to Trim the contents and other things:
function dropItems(idOfDraggedItem, targetId, x, y) {
var html = $("#dropContent").html();
html = $.trim(html);
if (html.length <= 0) {
var imageContent = $('<img src="images/drag.jpg" alt="" /><br />' + $("#" + idOfDraggedItem).html());
$("#dropContent").html(imageContent);
}
}
It worked for me in a test page I created.
The first line gets the html out of the element, second line trims it and thirth line checks if it is empty.
Forth line creates an element with the html string and the html content of the idOfDraggedItem element.
Fifth line sets the html content of the dropContent to the html content of the recently created element.
That's it.
Hope this works for you too.
PS the .html() method of jQuery extracts the innerHtml of the element, if you want the outerHtml, wrap the element in a div and get the .html() from div to get the outHtml of the wrapped element.