This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
InnerText alternative in mozilla
I have this code
<div id="code">My program<br />It is here!</div>
<script type="text/javascript">
var program=document.getElementById('code');
ShowLMCButton(program.innerText);
</script>
It works in IE but in firefox, innerText does not work. How can I use this in firefox? I have tried .text() but it doesn't want to work! I need the text to be in the form "My program\n It is here!" It can't be textContent because that copies the html tags.
What this is, is to copy a VB script from a site and paste it straight into a program and it must include all the new lines, white space etc.
ShowLMCButton() is a script that is "Click to Copy" - http://www.lettersmarket.com/view_blog/a-3-copy_to_clipboard_lmcbutton.html
use textContent for firefox
ShowLMCButton(program.innerText || program.textContent)
You can use JQuery to do this:
Live Demo
ShowLMCButton($('#code').text());
Related
This question already has answers here:
link to Print in a webpage
(2 answers)
Closed 8 years ago.
I am trying to add a print button for a form on the website that I maintain for a voluntary organization in Scotland. I would stress that I am not an expert in either HTML or Javascript but do my best.
The answer given by Murali to question 20101409 on 20th November 2013 looks as if it should work but does not. I suspect that I not named or defined something correctly.
My test webpage is: http://clanmacthomas.co.uk/Pages/TestCalculations.aspx
I strongly discourage the use <a href="javascript:..something.."> for any purpose, and especially the use of links to perform actions, preferring buttons instead.
If you have
<div>stuff</div>
<div>more stuff</div>
<input type="button" id="print" value="Print this page">
You can later attach a handler to it by using
<script>
document.getElementById("print").addEventListener("click", function() {
if (window.print) window.print();
});
</script>
If you are likely to want a print button on more than that one page, put that code in a .js file
/* printer.js */
document.getElementById("print").addEventListener("click", function() {
if (window.print) window.print();
});
There are nicer ways to do it if you are using jQuery, but you didn't mention that so I'm avoiding it.
You may want to read the SO question "Correct usage of addEventListener() / attachEvent()" since older versions of Internet Explorer use attachEvent instead of addEventListener.
To add a link that will open the print dialog on a webpage, include this link somewhere:
Print
This question already has answers here:
How to insert HTML as Text
(5 answers)
Closed 8 years ago.
div.innerHTML = my_text;
The code above has a problem: HTML tags are rendered. I want the opposite behavior: I want <b> to render as the string, not making anything bold. I also want to be able to use "\t" and "\n" as newline and tab characters. What can I do?
To prevent HTML from being rendered, simply escape it:
div.innerHTML = my_text.replace(/</g,"<");
This will still allow HTML entities such as é from being parsed. If you also want these to appear raw, tag this to the end of the line:
.replace(/&/g,"&");
Alternatively, try this "cleaner" option:
while(div.firstChild) div.removeChild(div.firstChild);
div.appendChild(document.createTextNode(my_text));
You are using div.innerHTML when you should be using div.innerText.
innerText is not cross browser, I recommend you to use a library like jQuery so you don't have this type of issues:
using jQuery you could write something like
$('#you_div').text(str.replace(/\t/g," "));
The replace above replaces your '\t' to "\t" so you will see \t on the text.
Anyway the standard seems to be the property textContent.
Here is some info in the MDN.
A different solution that better suites your needs probably is to get the text encoded and then replace the '\t' with , in this solution use the property innerHTML instead.
This question already has answers here:
focus() not working in safari or chrome
(6 answers)
Closed 3 years ago.
when my php page loads some jQuery should set focus to my input textbox but it does not
I have tried css selectors like input[tabindex=1] and #31 but to no avail...
here is the jQuery (version 1.7.1)
$(function() {
$('input[tabindex=1]').focus(); // also tried $('#31').focus();
});
and here is the HTML
<input tabindex='1' type='text' class='tablefilter' id='31' style='width:80px;border:inset 2px grey;background-color:white;font-size:12px;border-radius:0px;-moz-border-radius:0px;-webkit-border-radius:0px;'/>
any clues as to why this isn't working?
check this out, i just tried your code, it works. which version of jquery are you using? which browser are you using, try open console to see, if there is an exception.
here is the demo: DEMO
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert tags to html entities
I am building a snippets area to a friend's site, and i need to know how to convert html tags to normal text so the page will show the tags.
Like here in the code:
<img>
so the tag will be seen and not become to a normal html tag,
And all this using javascript or jQuery.
What I got now is something like that:
<pre><code>THE USERS CODE</code></pre>
and it still hide the html tags and make them active.
thx :)
You could use
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
SOURCE: http://css-tricks.com/snippets/javascript/htmlentities-for-javascript/
Use Jquery and instead of $('something').html() use $('something').text() checkout http://api.jquery.com/text/
This question already has answers here:
Can I create script tag by jQuery?
(5 answers)
Closed 9 years ago.
Is it possible to get JavaScript script tags inside jQuery .html() function?
function pleaseWork(){
$('#content').html('<h3 style="color:#335c91">This is the header</h3><div class="holder"><script type="text/javascript" src="javascriptFile.js"></script></div>');
}
What I am trying to accomplish is when the user clicks on a button, this header and JavaScript code show up in the div content, right now when I click on the button, there is no header showing and it goes to a blank page and shows the JavaScript code (it works, but not how I would like it to work.)
Escape the </script> tag, by replacing it with this: <\/script>
If that doesn't work, also surround the script with cdata tags.
Working demo: http://jsfiddle.net/BgxZN/1/
EDIT:
If you want to actually show the contents of the javascript file inside the div, you'll need to use an XMLHTTP request or an iframe.
New working demo: http://jsfiddle.net/BgxZN/13/