Unable to properly run an js object [duplicate]










0
















This question already has an answer here:



  • JavaScript closure inside loops – simple practical example

    39 answers



I made 5 buttons, an array object which has countries names, and an empty paragraph.



I want the innerHTML of the paragraph to display the corresondent name based on index of a for loop I implemented, such a way that if I click the first button the paragraph display the first country in the array and so on. But I this error in console:




Cannot read property 'country' of undefined




this is the html:



<p id="par"></p>
<button class="but" id="1">1</button>
<button class="but" id="2">2</button>
<button class="but" id="3">3</button>
<button class="but" id="4">4</button>
<button class="but" id="5">5</button>


and this is the js:



var bArr = $(".but")
var paises = [
country: "India", growth: 35,
country: "Indonesia",growth: 30,
country: "Russia",growth: 25,
country: "Mars",growth: 20,
country: "Pluton",growth: 15
]

for (i = 0; i< bArr.length; i++)
$(bArr[i]).click(function()
$("#par").html(paises[i].country)
)



Please, can anyone help me figure out what's the problem?



Thanks










share|improve this question















marked as duplicate by epascarello javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 17:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















    0
















    This question already has an answer here:



    • JavaScript closure inside loops – simple practical example

      39 answers



    I made 5 buttons, an array object which has countries names, and an empty paragraph.



    I want the innerHTML of the paragraph to display the corresondent name based on index of a for loop I implemented, such a way that if I click the first button the paragraph display the first country in the array and so on. But I this error in console:




    Cannot read property 'country' of undefined




    this is the html:



    <p id="par"></p>
    <button class="but" id="1">1</button>
    <button class="but" id="2">2</button>
    <button class="but" id="3">3</button>
    <button class="but" id="4">4</button>
    <button class="but" id="5">5</button>


    and this is the js:



    var bArr = $(".but")
    var paises = [
    country: "India", growth: 35,
    country: "Indonesia",growth: 30,
    country: "Russia",growth: 25,
    country: "Mars",growth: 20,
    country: "Pluton",growth: 15
    ]

    for (i = 0; i< bArr.length; i++)
    $(bArr[i]).click(function()
    $("#par").html(paises[i].country)
    )



    Please, can anyone help me figure out what's the problem?



    Thanks










    share|improve this question















    marked as duplicate by epascarello javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Nov 15 '18 at 17:23


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      0












      0








      0









      This question already has an answer here:



      • JavaScript closure inside loops – simple practical example

        39 answers



      I made 5 buttons, an array object which has countries names, and an empty paragraph.



      I want the innerHTML of the paragraph to display the corresondent name based on index of a for loop I implemented, such a way that if I click the first button the paragraph display the first country in the array and so on. But I this error in console:




      Cannot read property 'country' of undefined




      this is the html:



      <p id="par"></p>
      <button class="but" id="1">1</button>
      <button class="but" id="2">2</button>
      <button class="but" id="3">3</button>
      <button class="but" id="4">4</button>
      <button class="but" id="5">5</button>


      and this is the js:



      var bArr = $(".but")
      var paises = [
      country: "India", growth: 35,
      country: "Indonesia",growth: 30,
      country: "Russia",growth: 25,
      country: "Mars",growth: 20,
      country: "Pluton",growth: 15
      ]

      for (i = 0; i< bArr.length; i++)
      $(bArr[i]).click(function()
      $("#par").html(paises[i].country)
      )



      Please, can anyone help me figure out what's the problem?



      Thanks










      share|improve this question

















      This question already has an answer here:



      • JavaScript closure inside loops – simple practical example

        39 answers



      I made 5 buttons, an array object which has countries names, and an empty paragraph.



      I want the innerHTML of the paragraph to display the corresondent name based on index of a for loop I implemented, such a way that if I click the first button the paragraph display the first country in the array and so on. But I this error in console:




      Cannot read property 'country' of undefined




      this is the html:



      <p id="par"></p>
      <button class="but" id="1">1</button>
      <button class="but" id="2">2</button>
      <button class="but" id="3">3</button>
      <button class="but" id="4">4</button>
      <button class="but" id="5">5</button>


      and this is the js:



      var bArr = $(".but")
      var paises = [
      country: "India", growth: 35,
      country: "Indonesia",growth: 30,
      country: "Russia",growth: 25,
      country: "Mars",growth: 20,
      country: "Pluton",growth: 15
      ]

      for (i = 0; i< bArr.length; i++)
      $(bArr[i]).click(function()
      $("#par").html(paises[i].country)
      )



      Please, can anyone help me figure out what's the problem?



      Thanks





      This question already has an answer here:



      • JavaScript closure inside loops – simple practical example

        39 answers







      javascript jquery arrays






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 17:23









      Mohammad

      15.8k123765




      15.8k123765










      asked Nov 15 '18 at 17:16









      Julio RodriguezJulio Rodriguez

      978




      978




      marked as duplicate by epascarello javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 15 '18 at 17:23


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by epascarello javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 15 '18 at 17:23


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          1 Answer
          1






          active

          oldest

          votes


















          3














          You don't need to use a for loop. Add a click event listener to all buttons and in the event handler get the id attribute of the clicked button and use that to select the relevant item of array.






          var paises = [
          country: "India", growth: 35,
          country: "Indonesia",growth: 30,
          country: "Russia",growth: 25,
          country: "Mars",growth: 20,
          country: "Pluton",growth: 15
          ];
          $(".but").click(function()
          $("#par").text(paises[this.id-1].country);
          );

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <p id="par"></p>
          <button class="but" id="1">1</button>
          <button class="but" id="2">2</button>
          <button class="but" id="3">3</button>
          <button class="but" id="4">4</button>
          <button class="but" id="5">5</button>








          share|improve this answer































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            You don't need to use a for loop. Add a click event listener to all buttons and in the event handler get the id attribute of the clicked button and use that to select the relevant item of array.






            var paises = [
            country: "India", growth: 35,
            country: "Indonesia",growth: 30,
            country: "Russia",growth: 25,
            country: "Mars",growth: 20,
            country: "Pluton",growth: 15
            ];
            $(".but").click(function()
            $("#par").text(paises[this.id-1].country);
            );

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <p id="par"></p>
            <button class="but" id="1">1</button>
            <button class="but" id="2">2</button>
            <button class="but" id="3">3</button>
            <button class="but" id="4">4</button>
            <button class="but" id="5">5</button>








            share|improve this answer





























              3














              You don't need to use a for loop. Add a click event listener to all buttons and in the event handler get the id attribute of the clicked button and use that to select the relevant item of array.






              var paises = [
              country: "India", growth: 35,
              country: "Indonesia",growth: 30,
              country: "Russia",growth: 25,
              country: "Mars",growth: 20,
              country: "Pluton",growth: 15
              ];
              $(".but").click(function()
              $("#par").text(paises[this.id-1].country);
              );

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <p id="par"></p>
              <button class="but" id="1">1</button>
              <button class="but" id="2">2</button>
              <button class="but" id="3">3</button>
              <button class="but" id="4">4</button>
              <button class="but" id="5">5</button>








              share|improve this answer



























                3












                3








                3







                You don't need to use a for loop. Add a click event listener to all buttons and in the event handler get the id attribute of the clicked button and use that to select the relevant item of array.






                var paises = [
                country: "India", growth: 35,
                country: "Indonesia",growth: 30,
                country: "Russia",growth: 25,
                country: "Mars",growth: 20,
                country: "Pluton",growth: 15
                ];
                $(".but").click(function()
                $("#par").text(paises[this.id-1].country);
                );

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <p id="par"></p>
                <button class="but" id="1">1</button>
                <button class="but" id="2">2</button>
                <button class="but" id="3">3</button>
                <button class="but" id="4">4</button>
                <button class="but" id="5">5</button>








                share|improve this answer















                You don't need to use a for loop. Add a click event listener to all buttons and in the event handler get the id attribute of the clicked button and use that to select the relevant item of array.






                var paises = [
                country: "India", growth: 35,
                country: "Indonesia",growth: 30,
                country: "Russia",growth: 25,
                country: "Mars",growth: 20,
                country: "Pluton",growth: 15
                ];
                $(".but").click(function()
                $("#par").text(paises[this.id-1].country);
                );

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <p id="par"></p>
                <button class="but" id="1">1</button>
                <button class="but" id="2">2</button>
                <button class="but" id="3">3</button>
                <button class="but" id="4">4</button>
                <button class="but" id="5">5</button>








                var paises = [
                country: "India", growth: 35,
                country: "Indonesia",growth: 30,
                country: "Russia",growth: 25,
                country: "Mars",growth: 20,
                country: "Pluton",growth: 15
                ];
                $(".but").click(function()
                $("#par").text(paises[this.id-1].country);
                );

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <p id="par"></p>
                <button class="but" id="1">1</button>
                <button class="but" id="2">2</button>
                <button class="but" id="3">3</button>
                <button class="but" id="4">4</button>
                <button class="but" id="5">5</button>





                var paises = [
                country: "India", growth: 35,
                country: "Indonesia",growth: 30,
                country: "Russia",growth: 25,
                country: "Mars",growth: 20,
                country: "Pluton",growth: 15
                ];
                $(".but").click(function()
                $("#par").text(paises[this.id-1].country);
                );

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <p id="par"></p>
                <button class="but" id="1">1</button>
                <button class="but" id="2">2</button>
                <button class="but" id="3">3</button>
                <button class="but" id="4">4</button>
                <button class="but" id="5">5</button>






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 17:23









                Mark Meyer

                39.5k33162




                39.5k33162










                answered Nov 15 '18 at 17:19









                MohammadMohammad

                15.8k123765




                15.8k123765















                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto