Copied to clipboard

Flag this post as spam?

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


  • crisweb 13 posts 83 karma points
    Apr 16, 2020 @ 15:53
    crisweb
    0

    javascript inside foreach

    Hello this script works oddly well, but the code is not correct, how can I not loop the script? I ask for help please thanks

    @foreach(var kk in itemmenu) {

    <script type="text/javascript">
        $(document).ready(function () {
         $('#@kk.id').click(function(){ 
                $("#td").fadeOut(1);
                $("#td").fadeIn(1000);
                $.get('@kk.Url', function (data) {
                $('#td').html(data);        
            });
        });
    });
    

    }

  • Alex Skrypnyk 5730 posts 21691 karma points MVP 3x admin c-trib
    Apr 16, 2020 @ 16:25
    Alex Skrypnyk
    0

    Hi,

    Can you show all the code? And maybe try to see the source code of the page - what was rendered.

  • crisweb 13 posts 83 karma points
    Apr 16, 2020 @ 18:44
    crisweb
    0

    Hi In this image there is the code generated by the script and in my opinion even if executed it is not good. Quite right? enter image description here

  • Alex Skrypnyk 5730 posts 21691 karma points MVP 3x admin c-trib
    Apr 16, 2020 @ 19:00
    Alex Skrypnyk
    0

    Yes, this is a bad idea to render script per each id.

  • Alex Skrypnyk 5730 posts 21691 karma points MVP 3x admin c-trib
    Apr 16, 2020 @ 19:07
    Alex Skrypnyk
    0

    render all data in html and create just one script that uses data from HTML and do what you need

  • crisweb 13 posts 83 karma points
    Apr 16, 2020 @ 19:10
    crisweb
    0

    yes right, but I can't do it, could you give me some advice how to do it? please, I can't. thank you

  • Alex Skrypnyk 5730 posts 21691 karma points MVP 3x admin c-trib
    Apr 16, 2020 @ 19:10
    Alex Skrypnyk
    100

    Something like this:

    @foreach(var kk in itemmenu)
    {   
        <input type="button" class="btn btn-success" id="@kk.id" value="@kk.Name" data-url="@kk.Url" />     
    }
    
    <script type="text/javascript">
        $(document).ready(function () {
            $('.btn-success').click(function(){ 
                $("#td").fadeOut(1);
                $("#td").fadeIn(1000);
                var url = $(this).data('url');
                $.get(url, function (data) {
                    $('#td').html(data);        
                });
            });
        });
    </script>
    
  • crisweb 13 posts 83 karma points
    Apr 16, 2020 @ 19:16
    crisweb
    0

    I tried your script, IT WORKS PERFECTLY, Thank you very much !!! I hope to be of assistance in the future. Thanks again Sorry my english is terrible

  • Alex Skrypnyk 5730 posts 21691 karma points MVP 3x admin c-trib
    Apr 16, 2020 @ 19:19
    Alex Skrypnyk
    0

    You are always welcome!!! Ask more questions!

  • crisweb 13 posts 83 karma points
    Apr 25, 2020 @ 11:33
    crisweb
    0

    Hello Can I ask you a favor? the script you corrected me you manage a menu, could you kindly implement it by making sure that the first menu item is open as you enter the page? Thank you so much

  • crisweb 13 posts 83 karma points
    Apr 25, 2020 @ 11:34
    crisweb
    0

    I don't understand much of javascript

  • crisweb 13 posts 83 karma points
    Apr 25, 2020 @ 13:10
    crisweb
    0

    Hi this assigns the right css display: block class, but the div is not populated because it populates at the click event. I don't know what to do please

    <script type="text/javascript">
    $(document).ready(function () {
        var $q = $("#td"); 
        $q.css("display", $q.css("display") === 'block' ? 'block' : 'block');
        $('.btn-success').click(function(){ 
            $("#td").fadeOut(1);
            $("#td").fadeIn(1000);
            var url = $(this).data('url');
            $.get(url, function (data) {
                $('#td').html(data);  
            });
        });
    });
    

  • crisweb 13 posts 83 karma points
    Apr 25, 2020 @ 13:48
    crisweb
    0

    Sorry again this code works, but is it correct? what do you think about it? thanks

    <script type="text/javascript">
    $(document).ready(function myFunction() {
        $("#td").load("/materiali-scaricabili/brochure/");
        <!--alert("Page is loaded");-->
        var $q = $("#td"); 
        $q.css("display", $q.css("display") === 'block' ? 'block' : 'block');
        $('.btn-success').click(function(){ 
            $("#td").fadeOut(1);
            $("#td").fadeIn(1000);
            var url = $(this).data('url');
            $.get(url, function (data) {
                $('#td').html(data);  
            });
        });
    });
    

  • Alex Skrypnyk 5730 posts 21691 karma points MVP 3x admin c-trib
    29 days ago
    Alex Skrypnyk
    0

    Can you show me your html as well?

Please Sign in or register to post replies

Write your reply to:

Draft