-1

I have an array. But I can't print that array at all. I am almost madly for this type of array behaviour. Please help me how to print that array.

My Array

It seems to be an empty array. But It is an array. When I click that array I see that result.

enter image description here

When I move cursor the icon i it gives me a message.

value below was evaluated just now

This is my code

var selectedVal = [];

$('body').on('click', '.carat ', function () {
    $(".single-progress-mark").show();
    $('.carat ').removeClass("active");

    $(this).addClass("active");
    var det = $(this).attr('data-attr-details');
    async: false;
    $('.Carat1').text(det);
    selectedVal.push({
        Carat : det
    });
});

console.log(selectedVal);

This is my console.log output. But I want to print 0.30. I want many ways to print that.

like console.log(selectedVal[0].carat); or console.log(selectedVal[0]['carat']); It always gives me undefined or error.

So please help me how to print my desired output 0.30

A.A Noman
  • 5,244
  • 9
  • 24
  • 46
  • What happens when you put `console.log(selectedVal)` inside the body click event at last? – palaѕн Apr 26 '20 at 14:39
  • You need to move the console.log into the click handler function – gavgrif Apr 26 '20 at 14:43
  • @palaѕн Then It shows the correct answer. But I need this out of the click function – A.A Noman Apr 26 '20 at 14:43
  • You can't because you are basically asking. I need to display a user name in the console log which is saved in the database before I could even make an ajax call to fetch the data. – palaѕн Apr 26 '20 at 14:47
  • @palaѕн But I get the result out of the click handler. But how do I print this value. Please some suggestion if alternative way – A.A Noman Apr 26 '20 at 14:52
  • There is no way other than moving the console.log into the click handler function. For more info check: [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – palaѕн Apr 26 '20 at 14:56

1 Answers1

0

I have solve this problem by help of @palash advice

$('body').on('click', '.carat ', function () {
    $(".single-progress-mark").show();
    $('.carat ').removeClass("active");

    $(this).addClass("active");
    var det = $(this).attr('data-attr-details');
    async: false;
    $('.Carat1').text(det);
    var selectedVal = [];
    selectedVal.push({
        Carat : det
    });
    passValue(selectedVal);
});

And in my function I get this value

function passValue(passParameter){
    console.log(passParameter[0].Carat);
}
A.A Noman
  • 5,244
  • 9
  • 24
  • 46