Thanks for helping. Yes I have tried this. It works if i create my own static c# var. But when using Umbraco.Field i am getting error. I have tried making it Html.Raw also....
Anders - you don't say what the error is? The more detail you can provide the more likely you will get a solution on here otherwise its based largely on guess work.
Along the lines of what John suggested - although John your example returns an object type and not a string. Try using something like this?
Like the guys have already written it's a lot easier to provide better tips and suggestions the more you share about the scenario you need to handle by adding the Rich text to a JavaScript variable.
What do you need the rich text for? What would the end result be? The reason why it works when using a textbox multiline property type is probably because it's raw text that is returned, which does not contain characters like "<>' etc. etc. which you need to escape in your JavaScript.
But if you share some more information about what you're trying to achieve it could be that there is another way around it that may be even better :)
@{
Layout = null;
var n = Html.Raw(Model.Content.GetPropertyValue<string>("bodyText").Replace("\r", " ").Replace("\n", string.Empty));
}
<script>
function getValue() {
document.getElementById('message').innerHTML = '@n';
}
</script>
<button id="myHeader" onclick="getValue()">Click me!</button>
<div id="message"></div>
Thanks Simon. A further suggest would be to combine the two solutions. The above example should work so if it fails check to make sure it's not you data missing/queried incorrectly. Also try !=null instead of IsNull
Another simple approach could be to simply render the content from the RTE using razor and wrap it in a div with an id called "error" for instance and then initially hide it with CSS using display:none and then when the error occurs simply use JavaScript changing the display property to block.
using Umbraco field from richtexteditor in js codes
How can I use some content from a richtexteditor as a js var?
I have tried this and much more :-):
@{
var text = @Umbraco.Field("text", recursive: true)
}
<script>
var text = '@text';
</script>
Thanks
Hi Anders
What is your scenario? Why do you need to have RTE in your JS? Just want to make sure I understand what you're trying to achieve.
/Jan
I have some html from richtexteditor.
This I will need to be able on appending from js.
Thanks
have you tried?
<script>@{<text>var text = '@text';</text>}</script>
or
@Model.Content.GetPropertyValue("text", true)
Hi John
Thanks for helping. Yes I have tried this. It works if i create my own static c# var. But when using Umbraco.Field i am getting error. I have tried making it Html.Raw also....
Thanks
Hi Anders,
Like Jan said it would be useful to know the context and more broadly what your trying to achieve.
Thanks
Hi John
I am calling my content to page using ajax call. If service returns empty call I will Insert some standard html from Umbraco backend.
Make sense
/Anders
UPDATE.
I found out that if i change umbraco field into normal multiline textstring There is no problem. Any sugestions ?
real stab in the dark but have you tried removing carrage returns/new line? .replace("\r", " ").replace("\n", string.Empty)
Anders - you don't say what the error is? The more detail you can provide the more likely you will get a solution on here otherwise its based largely on guess work.
Along the lines of what John suggested - although John your example returns an
object
type and not a string. Try using something like this?Simon
Hi Anders
Like the guys have already written it's a lot easier to provide better tips and suggestions the more you share about the scenario you need to handle by adding the Rich text to a JavaScript variable.
What do you need the rich text for? What would the end result be? The reason why it works when using a textbox multiline property type is probably because it's raw text that is returned, which does not contain characters like "<>' etc. etc. which you need to escape in your JavaScript.
But if you share some more information about what you're trying to achieve it could be that there is another way around it that may be even better :)
Looking forward to hearing from you.
/Jan
Hi
Thanks for being helpfull. And sorry for not giving enough information.
I Would like to have a richtexteditor on my umbraco backend.From there I would get some html
Example: <h2>Sorry</h2><p>There has been a mistake</p>
I am building this content with ajax:
.....
success:function(data){
if(data IsNull){
$('#meaasge').html('[MyTextFromRichTextEditorInUmbraco]')
}
.....
I hope this make sence :-)
Thanks
Hi Simon
Another simple approach could be to simply render the content from the RTE using razor and wrap it in a div with an id called "error" for instance and then initially hide it with CSS using display:none and then when the error occurs simply use JavaScript changing the display property to block.
Just my 2 cents.
/Jan
is working on a reply...