How to get the whole HTML value from Dijit Editor - javascript

Hello Experts I need help
I'm using dojo Dijit Editor "rich text editor" field in my widget, on page load I fetch HTML text from database and I set the rich text editor with the HTML normally, Then user can edit the displayed text and on page close I have to set the field in database with the source HTML of the edited text by user
the problem is when I do the following "myDB_txt=myEditor.getValue();" getValue() doesn't return the complete HTML code it removes HTML tag and header tag and body tag which causes me troubles.

Simply use myEditorWidget.get("Value") where myEditorWidget refer to your dijit/Editor indtance
To wrap this result you can define a function that return result wraped by html tags
wrapResult(myEditor.get("value")));
function wrapResult(html) {
return "<html> <head></head> <body>"+html+"</body></html>";
}
Here is a Sample with wraped result Fiddle .
Otherwise If you want to get the whole HTML enclosing the content dijit ,
you will get access to it's Iframe ( that has the id="editor_iframe")
and then get get the html document of this last like bellow (here you should import dojo/query package)
query("#editor_iframe")[0].contentDocument.documentElement.outerHTML
Here is another Fiddle .

You could try the following to retrieve the value from your dijit/Editor instance.
var content = myEditor.attr("value");
var openTags = '<html><head></head><body>';
var closeTags = '</body></html>';
var html = openTags + content + closeTags; // use this
or
var htmlWrapper = function(content){
return '<html><head></head><body>' + content + '</body></html>';
};
var html = htmlWrapper(myEditor.attr("value"));

Related

In php script code document.createElement('p').textContent is not null or something like that, but it won't display the created element. Why?

I'm new in php programming. I'm in a very interesting moment.
let str = 'anytext';
line = document.createElement('p');
line.textContent = str;
alert('line = ' + line.textContent);
I tried to use and .textContent and .innerHTML and .innerText. Nothing. It refuses to display anything.
The alert shows that the value successfully assigned. No errors, but there is nothing displayed on
the screen of the browser. I mean, no paragraph element created visually.
I use javascript not in html document. I use it in php file.
The page has a table, which I created programmatically with php.
I thought I can solve this problem by sending the
$html = new DOMDocument('1.0','iso-8859-1');
with json_encode() and JSON.parse(); to javascript. But it didn't help too.
I don't know what else can I do. Can you help me?
You've created an element, but you haven't inserted it anywhere into the document. You need to figure out the appropriate place to put it into the DOM and then do so.
For example, if you have existing HTML of
<div class="main">
</div>
you can create a <p> and insert it to the bottom of the .main with
const main = document.querySelector('.main');
const line = document.createElement('p');
line.textContent = 'anytext';
main.appendChild(line);

Can't change span content inside a td HTML using js/php

I have a php snippet on Wordpress that contains a loop through my SQL that will create a table for user.
On that PHP code, i have a specific var that needs to get a X value from web, i was using PHP to do all of it, but since it's not async, it causes page load to be 10s~
I tried use jQuery/JS, unfortunately, i couldn't change value inside <td>
$charstatus = "
<script>
$.get('isOnline.php?name=".$jsName."', function(data) {
var element = document.querySelector('".$vps.$slot."')
element.innerHTML = data
});
</script>";
Why won't it work?

Html and Javascript send email with text element

I have a form on sharepoint, that whenever a person fills that form, it will create a new page with the text added on the form.
I wanted to send an email using JavaScript and HTML with the copy of the various texts.
I have the id of the text and when i create the javascript code, it does not work.
HTML
<a onclick="javascript:sendEmailJobs()" class="mailto"></a>
JavaScript
function sendEmailJobs(){
var JobCodePost = document.getElementById("RichHtmlFieldJobPostCodeJob").body;
window.location.href = "mailto:?subject=Job Offer &body=" + body;
}
The id of said text is this:
<SharepointWebControls:NoteField ID="RichHtmlFieldJobPostCodeJob" FieldName="JobPostCode" runat="server"/>
Am i doing something wrong?
Document elements don't have a "body" attribute, they have innerHTML and innerText. So your function should look like this:
function sendEmailJobs(){
var JobCodePost = document.getElementById("RichHtmlFieldJobPostCodeJob").innerText;
window.location.href = "mailto:?subject=Job Offer &body=" + JobCodePost;
}
Second thing is to verify that you are indeed selecting the correct element. Third thing is to point out that you can't use innerHTML because even if you get everything else right, mailto: links don't support formatted text.

jquery dom object to string

i have a textarea with contents of html file. The textarea content includes my entire html page content, includes the doctype, head, html etc.
now, i want to save the textarea into a dom vairable via $.parseHTML:
var txt = $.parseHTML($("textarea").val(), null, true);
next, i change my txt variable and dom elements with commands like:
$(txt).find("h1").text("demo");
$(txt).find("h2").text("demo h2");
after changing the txt dom i want to put the txt as string back to the textarea, but nothing seems to work. i already tried:
$("textarea").val($(txt).html());
or
$("textarea").val(txt.html());
i also tried looping the dom:
$.each($(txt).get(),function(){
v = $("textarea").val();
$("textarea").val(v+$(this)[0].outerHTML);
});
but that didn't gave the expected results as items outside the body tag haven't translated back.
any advise shall be greatly appriciated
var txt = $('#textarea').val();
var html = $('<div class="wrap">'+txt+'</div>');
$('h1', html).text('is H1');
$('h2', html).text('is H2');
$('#textarea').val(html.html());`

javascript: extracting text from html

I am getting html content as below:
var test='<div id="test">Raj</div>';
How can i retrieve value Raj from above html content using javascript.
It sounds like you're trying to extract the text "Raj" from that HTML snippet?
To get the browser's HTML parser to do your dirty work for you:
// create an empty div
var div = document.createElement("div");
// fill it with your HTML
div.innerHTML = test;
// find the element whose text you want
test = div.getElementById("test");
// extract the text (innerText for IE, textContent for everyone else)
test = test.innerText || test.textContent;
Or in jQuery:
test = $(test).text();
If you use jQuery (I cannot believe I just said that ;)) you can get at the content immediately you wrap it in $(test).html
Someone else will tell you how to get at the innerHTML using a selector since everybody here are jQuery gurus but me
Update: somebody just did while I was editing: javascript: extracting text from html - see comments or updates to that
var test = getElementById('test')
try that

Categories

Resources