1

How do i get the data object from a html Object that has more than one like element in an array.

<div id='show_box'>
    <h6 id="#0" data-choosed="1000">1000</h6>
    <h6 id="#1" data-choosed="1000">2000</h6>
    <h6 id="#2" data-choosed="1000">3000</h6>
</div> 

In the javascript var h6_len=$("#show_box > h6").length;

         switch (h6_len) {
            case 0:
                 choosed=$('#show_box > #' + h6_len).data('choosed');

                $('#total_box').text(choosed);// this return the drop-down clicked
              //this part of the code is working fine.. when the case == 0
              break;
        case 1:
              var h6_1=$('#show_box > h6')[0];
                  var h6_2=$('#show_box > h6')[1];

                  /* this is where i am having issues... getting the data value from one of the array the H6 element...

                  console.log( typeof h6_1);
                  break;
            case 2:
                  console.log(h6_len);
                  break;
                  default:
                  $('#total_box').empty();
          }

1 Answer 1

1

HTML

<div id='show_box'>
    <h6 id="#0" data-choosed="1000">1000</h6>
    <h6 id="#1" data-choosed="2000">2000</h6>
    <h6 id="#2" data-choosed="3000">3000</h6>
</div>

jQuery

$('h6').each(function () {
    console.log($(this).data('choosed'));
});

Working fiddle. Hopefully this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

var h6_1=$('#show_box > h6')[0]; var h6_2=$('#show_box > h6')[1]; /* this is where i am having issues... getting the data value from one of the array the H6 element...
when the case is one there are 2 html objects h6_1=$('#show_box > h6')[0];var h6_2=$('#show_box > h6')[1]; that is why i made it in an array but i want to get the data value "$('#show_box > h6')[0].data('choosed')" but it returns undefined. please what can i do to get the data from the two object.
@user3223851 It would be great if you can create a fiddle to show when is switch case fired and show the error in the console..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.