Here is a sample code what I am trying to fix. In print screen I am not able to see the text which is entered in input text field. Any solution will be appreciated.
var divToPrint=document.getElementById('div1Id');
if (divToPrint != null && divToPrint != "") {
var newWin=window.open('','Print-Window');
newWin.document.open();
newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
newWin.document.close();
newWin.close();
}
use jquery.print.js library and call this function in your custom function $.print("#id");
Textbox and Checkbox controls you have to get value instead of InnerHtml
var divName = document.getElementById('div1Id')
var originalContents = document.body.innerHTML;
var inpText = document.getElementsByTagName("input")[0].value;
var printContents = document.getElementById(divName).innerHTML;
printContents += inpText;
newWin.document.open();
newWin.document.write('<html><body
onload="window.print()">'+printContents+'</body></html>');
newWin.document.close();
You have this script
var divToPrint=document.getElementById('div1Id');
if (divToPrint != null && divToPrint != "") {
var newWin=window.open('','Print-Window');
newWin.document.open();
newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
newWin.document.close();
newWin.close();
}
where you copy the inner html, however, the value is not there. Let's make sure the values are correct, like this:
var divToPrint=document.getElementById('div1Id');
//Gather input values and inputs
var inputValues = [];
var inputs = divToPrint.getElementsByTagName("input");
for (var index = 0; index < inputs.length; index++) inputValues.push(inputs[index]);
//Generate script of page
var script = 'var inputValues = ' + JSON.stringify(inputValues) + ';';
script += 'var inputs = document.getElementsByTagName("input");' +
'for (var index = 0; index < inputs.length; index++) ' +
'inputs[index].value = inputValues[index];';
if (divToPrint != null && divToPrint != "") {
var newWin=window.open('','Print-Window');
newWin.document.open();
newWin.document.write('<html><body onload="' + script + 'window.print()">'+divToPrint.innerHTML+'</body></html>');
newWin.document.close();
newWin.close();
}
Code is untested, please let me know if there is a typo.
Related
I am a seller on eBay and I want to use JavaScript on eBay. I have successfully used document.write to put the source file of JavaScript. The problem is it not working
This is my JavaScript:
window.onload = function trackt(){
if (typeof this.href === "undefined") {var str = document.location.toString();}
else {str = this.href.toString();}
var pos = str.indexOf("itm/");
var newStr = str.substring(pos+4);
var pos1 = newStr.indexOf("-/");
var newStr1 = newStr.substring(0,pos1);
var newStr2 = newStr1.replace(/\-/g,' ')
document.getElementById("cssmenu1").innerHTML = newStr1;
}
This my css:
#cssmenu_box{}#cssmenu1{float:left;right:-2%;padding:9;width:940px;height:31px;font-size:25px;background:transparent url('http://www.endition.net/ebay/templates/001/images/menu_bg.jpg') repeat-x top left;font-family:'Trebuchet MS',Helvetica,Arial,Verdana,sans-serif;color:#FFFFFF;border: 0.5px solid;border-radius:10px;}
This is my html code:
<div id="cssmenu_right"></div><div id="cssmenu1">Men Leather Jacket</div>
.innerHTML is showing error when I run it using console. I have to use document.write() function.
window.onload = function trackt(){
if (typeof this.href === "undefined") {var str = document.location.toString();}
else {str = this.href.toString();}
var pos = str.indexOf("itm/");
var newStr = str.substring(pos+4);
var pos1 = newStr.indexOf("-/");
var newStr1 = newStr.substring(0,pos1);
var newStr2 = newStr1.replace(/\-/g,' ')
document.getElementById("cssmenu1").innerHTML = newStr1;
}
//instead of innerHTML use text(newStr1);
I have a rich:editor
<rich:editor id="editor" width="1000" height="300"
value="#{emailTemplateHome.instance.emailTemplateContent}"
theme="advanced">
</rich:editor>
and javascript function
function change(){
var val = document.getElementById("#{rich:clientId('editor')}");
if(val != null){
var component = val.component;
var content = component.tinyMCE_editor.getContent();
var position = content.slice(0, val.positionedOffset).length;
var result = content.substr(0,position) + "{chuc_vu}" + content.substr(position);
component.tinyMCE_editor.setContent(result);
}
}
but var position is not defined.
How can i get position of current cursor ???
Thanks in advance
If you only want to insert things at the current position you can use this:
component.tinyMCE_editor.execCommand('mceInsertContent', false, "insert text");
If you want to do something with the cursor position that's going to be tricky, since the position in the editor doesn't correspond to the position in the content (the content has HTML tags in it). You can get the position this way:
component.tinyMCE_editor.selection.getBookmark().start
val.positionedOffset is a function that returns an array, I'm not sure what you were using that for (not to mention that slice(0,x).length = x)
Writing something after cursor position:
function writeAfterCursor(element) {
var bm =tinyMCE.activeEditor.selection.getBookmark().start;
var content= tinyMCE.activeEditor
.getContent();
content= strip(content);
var res = content.split("");
var res1= [];
var res2= [];
for(i=0; i < bm; i++){
res1[i]=res[i];
}
var j=0;
for(i=bm; i < res.length; i++){
res2[j]=res[i];
j++;
}
var content1= res1.join("");
var content2= res2.join("");
content = content1+ element.value+content2;
tinyMCE.activeEditor.setContent(content);
tinyMCE.activeEditor.selection.moveToBookmark(bm);
}
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
I am trying to add values to a textbox when looping through an array when checking checkboxes but as it is at the moment getting undefined.
Advice perhaps as to why the values are 'undefined'
var txtBoxValues = [];
$(document).on("click", "input[name=chkRelatedTopics]", function () {
var nameAdminUser = $(this).val();
var txtBox = document.getElementById("txtTraningTopics");
txtBox.value = '';
txtBoxValues.push(nameAdminUser);
for (var i in txtBoxValues) {
var str = txtBoxValues[i].value;
txtBox.value += str + '; ';
}
});
nameAdminUser is already a string, so don't take .value from it.
You could replace
var str = txtBoxValues[i].value;
with
var str = txtBoxValues[i];
But instead of using this loop, and assuming you don't want, as I suppose, the last ";", you could also do
txtBox.value = txtBoxValues.join(';');
nameAdminUser seems to be a String and in your for loop you expect an object. What if you simply do:
for (var i in txtBoxValues) {
var str = txtBoxValues[i];
txtBox.value += str + '; ';
}
I am adding a row to a table, and attached an ondblclick event to the cells. The function addrow is working fine, and the dblclick is taking me to seltogg, with the correct parameters. However, the var selbutton = document.getElementById in seltogg is returning a null. When I call seltogg with a dblclick on the original table in the document, it runs fine. All the parameters "selna" have alphabetic values, with no spaces, special characters, etc. Can someone tell me why seltogg is unable to correctly perform the document.getElementById when I pass the id from addrow; also how to fix the problem.
function addrow(jtop, sel4list, ron4list) {
var tablex = document.getElementById('thetable');
var initcount = document.getElementById('numrows').value;
var sel4arr = sel4list.split(",");
var idcount = parseInt(initcount) + 1;
var rowx = tablex.insertRow(1);
var jtop1 = jtop - 1;
for (j = 0; j <= jtop1; j++) {
var cellx = rowx.insertCell(j);
cellx.style.border = "1px solid blue";
var inputx = document.createElement("input");
inputx.type = "text";
inputx.ondblclick = (function() {
var curj = j;
var selna = sel4arr[curj + 2];
var cellj = parseInt(curj) + 3;
inputx.id = "cell_" + idcount + "_" + cellj;
var b = "cell_" + idcount + "_" + cellj;
return function() {
seltogg(selna, b);
}
})();
cellx.appendChild(inputx);
} //end j loop
var rowCount = tablex.rows.length;
document.getElementById('numrows').value = rowCount - 1; //dont count header
} //end function addrow
function seltogg(selna, cellid) {
if (selna == "none") {
return;
}
document.getElementById('x').value = cellid; //setting up for the next function
var selbutton = document.getElementById(selna); //*****this is returning null
if (selbutton.style.display != 'none') { //if it's on
selbutton.style.display = 'none';
} //turn it off
else { //if it's off
selbutton.style.display = '';
} //turn it on
} //end of function seltogg
You try, writing this sentence:
document.getElementById("numrows").value on document.getElementById('numrows').value
This is my part the my code:
contapara=(parseInt(contapara)+1);
document.getElementById("sorpara").innerHTML+="<li id=\"inputp"+contapara+"_id\" class=\"ui-state-default\"><span class=\"ui-icon ui-icon-arrowthick-2-n-s\"></span>"+$('#inputp'+contapara+'_id').val()+"</li>";
Look you have to use this " y not '.
TRY!!!!
In this code I get the following error:Exception... "Index or size is negative or greater than the allowed amount" code: "1" nsresult: "0x80530001 (NS_ERROR_DOM_INDEX_SIZE_ERR)". What is causing this? Thanks
function makecard(){
var bodypaint = document.getElementById('minaj');
var recipient = document.getElementById("recipient").value
var radioboxes = document.forms["cardform"].phrase.length
var i = document.getElementById("color").selectedIndex;
var z = document.getElementById("city").selectedIndex;
var tchatche= document.getElementById("color").options[i].text;
var malouba= document.getElementById("city").options[z].value;
for(c=0; c<radioboxes; c++){
if( document.forms["cardform"].phrase[c].checked){
var phrasevalue=document.forms["cardform"].phrase[c].value;
break;
}
}
if(document.getElementById("color").options[i].text === "White"){
bodypaint.style.backgroundColor ="White"
}
if(document.getElementById("color").options[i].text === "Red"){
bodypaint.style.background ="Red"
}
if(document.getElementById("color").options[i].text === "Blue"){
bodypaint.style.backgroundColor ="Blue"
}
var selectedcity = document.forms["cardform"].city.value
var paragraph = document.createElement("div");
paragraph.setAttribute("id","card")
document.body.appendChild(paragraph)
var picture = document.createElement("img")
picture.setAttribute("src", "")
paragraph.appendChild(picture)
paragraph.appendChild(document.createTextNode(phrasevalue + " from " + selectedcity + recipient))
if(malouba== "Paris"){
document.getElementById("picture").src = "paris.jpg"
}
if(malouba== "Venice"){
document.getElementById("picture").src = "venice.jpg"
}
if(malouba== "Rome"){
document.getElementById("picture").src = "rome.jpg"
}
}
document.getElementById("makeacard").onclick = makecard;
It's highly likely that one of the statements is producing a array of 0 elements and then when you try to use it later it freaks out:
var radioboxes = document.forms["cardform"].phrase.length
do a little console.log(radioboxes) on that and see if it comes back undefined or empty