Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anders Schmidt 76 posts 207 karma points
    Feb 03, 2015 @ 14:17
    Anders Schmidt
    0

    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

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Feb 03, 2015 @ 14:29
    Jan Skovgaard
    0

    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

  • Anders Schmidt 76 posts 207 karma points
    Feb 03, 2015 @ 15:52
    Anders Schmidt
    0

    I have some html from richtexteditor.

    This I will need to be able on appending from js.

    Thanks

  • John Churchley 272 posts 1258 karma points c-trib
    Feb 04, 2015 @ 10:16
    John Churchley
    0

    have you tried?  

    <script>@{<text>var text = '@text';</text>}</script>       

    or

    @Model.Content.GetPropertyValue("text", true)

    <script type="text/javascript">// <![CDATA[
    </span><span style="font-size: 12px;">@{<text>var text = '@text';</text>}</span><span style="font-size: 12px;">
    // ]]></script>

  • Anders Schmidt 76 posts 207 karma points
    Feb 04, 2015 @ 10:26
    Anders Schmidt
    0

    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

  • John Churchley 272 posts 1258 karma points c-trib
    Feb 04, 2015 @ 10:30
    John Churchley
    0

    Hi Anders,

    Like Jan said it would be useful to know the context and more broadly what your trying to achieve.

    Thanks

  • Anders Schmidt 76 posts 207 karma points
    Feb 04, 2015 @ 10:34
    Anders Schmidt
    0

    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

  • Anders Schmidt 76 posts 207 karma points
    Feb 04, 2015 @ 11:57
    Anders Schmidt
    0

    UPDATE.

    I found out that if i change umbraco field into normal multiline textstring There is no problem. Any sugestions ?

  • John Churchley 272 posts 1258 karma points c-trib
    Feb 05, 2015 @ 10:13
    John Churchley
    0

    real stab in the dark but have you tried removing carrage returns/new line? .replace("\r", " ").replace("\n", string.Empty)

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Feb 05, 2015 @ 10:25
    Simon Dingley
    1

    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?

    @Html.Raw(Model.Content.GetPropertyValue<string>("propertyAlias", true))
    

    Simon

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Feb 05, 2015 @ 21:45
    Jan Skovgaard
    0

    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

  • Anders Schmidt 76 posts 207 karma points
    Feb 05, 2015 @ 21:54
    Anders Schmidt
    0

    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

  • John Churchley 272 posts 1258 karma points c-trib
    Feb 05, 2015 @ 23:19
    John Churchley
    1
     @{
            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
     
     
     
  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Feb 06, 2015 @ 05:23
    Jan Skovgaard
    1

    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

Please Sign in or register to post replies

Write your reply to:

Draft