Copied to clipboard

Flag this post as spam?

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


  • crisweb 18 posts 88 karma points
    Apr 25, 2018 @ 16:55
    crisweb
    0

    function javascript and razor

    Hi I have this function that works well:

        <script>
         function AssignButtonClicked(elem)
    {
        var id= $(elem).data('assigned-id');
        alert(id)
        document.getElementById("td").innerHTML = id;       
    }    
        </script>
    
    
    <div id="td"></div>
    

    it gives me the right id at the click.

    then I have this script:

    @{ int p = 1073;}                                                            
    <img src="@Umbraco.Content(@p).portFoto" />
    <h3>@Umbraco.Content(@p).portTitolo  </h3>
    @Umbraco.Content(@p).portTesto
    

    it returns me the right contents belonging to the node with id 1073.

    The question is: how do I assign the value returned by the javascript function to the variable p Thanks!

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Apr 26, 2018 @ 05:57
    Paul Seal
    0

    You could use an api controller to get that content, which you could call from your JavaScript.

  • crisweb 18 posts 88 karma points
    Apr 26, 2018 @ 16:11
    crisweb
    0

    Thank you are not very practical to write controllers, however it is not a problem to get the contents, the problem is the passage of the id, it is not possible in querystring because the href event is inhibited so you can only go in javascript, I miss how to pass id obtained in javascript, to be able to put it in a foreach or to get the right contents of the different nodes. Please help me !! thank you

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Apr 27, 2018 @ 05:17
    Paul Seal
    0

    You are missing the basic concept that the razor code runs server side before the JavaScript. The only way I can think of this working without some kind of api call is to load the contents into a JSON array using razor, then look up the array when you click on an element. You could store the id of the element in a data attribute if you're not doing it already.

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Apr 27, 2018 @ 07:50
    Steve Morgan
    0

    Hi Crisweb,

    Can you start from the requirement?

    I'm guessing you're swapping an image and content out depending on the element clicked? Is that right?

    This is possible without ajax if you have a limited number of options you could output them all and hide and show the right one depending on the click. I wouldn't suggest you do this if there are more than 40-50 odd as the page size would start to be a problem.

    This would avoid the need for a controller to return the ajax but Paul's suggestion is the cleaner solution. Let us know how many different IDs there are likely to be and I can skeleton you out a quick solution.

    Steve

  • crisweb 18 posts 88 karma points
    Apr 27, 2018 @ 15:15
    crisweb
    0

    Sorry but my mind is stuck !!!

    The idea was this:

    I find the images and send 'id unfortunately not in query string

    and it works

    @{ var portof1 = CurrentPage.AncestorOrSelf(1).Descendants("portfolio");} @foreach (var port1 in portof1.Where("visible")) {

    <li data-panel="panel-1" onclick=AssignButtonClicked(this) [email protected]  ><a href="#"><img src="@port1.portFoto" /></a></li>
    

    }

    I resume the id

    and it works

    function AssignButtonClicked(elem){
    var id= $(elem).data('assigned-id'); 
    //alert(id)
    document.getElementById("td").innerHTML = id; }<div id='td'></div> 
    

    here I do not know what to do !!!!

    with fixed id works well
    @{ int p = 1073;}

    @Umbraco.Content(@p).portTitolo

    @Umbraco.Content(@p).portTesto

    Please help me

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Apr 27, 2018 @ 15:28
    Steve Morgan
    0

    Hi,

    Sorry - but that makes no sense to me! What are you trying to do?

    Steve

  • crisweb 18 posts 88 karma points
    Apr 27, 2018 @ 19:09
    crisweb
    0

    Hello I'm trying to populate the variable @{int p = } with the result of javascript. Am I wrong to do it?

    Andrea

Please Sign in or register to post replies

Write your reply to:

Draft