2

I am trying to retrieve data from a variable HTML element. On click, the id of the <span> element is retrieved, which I want to enable me to dynamically $([dynamic id]) select that element and request the data stored in the data attribute.

My jQuery looks like this:

$( document ).ready( function() {
    $( ".checkmark" ).on( "click", ( event ) => {
        let checkBoxId = "#" + event.target.id, // #checkBox1
            checkBoxData = event.target.id + "-value", // checkBox1-value
            checkBoxValue = $( checkBoxId ).data( checkBoxData ); // undefined
    } );
} );

The HTML element targeted looks like this:

<span class="checkmark" id="checkBox1" data-checkBox1-value=-155></span>

The value of let checkBoxValue is undefined and I cannot figure out why. Help would be greatly appreciated.

5 Answers 5

1

You can get attribute value of span using attr() function in jQuery

checkBoxValue = $(checkBoxId).attr(checkBoxData);
Sign up to request clarification or add additional context in comments.

Comments

1

The checkBoxId variable is unnecessary because you can use the this keyword since it is the current element you are working with.

$(function() {
    $(".checkmark").on("click", (event) => {
        let checkBoxData = event.target.id + "-value";
        let checkBoxValue = $(this).data(checkBoxData);
    });
});

Comments

1

It seems you are having scope issues with the new ()=>{} syntax.

So, you will need to bind this to the function event handler using {self:this}. If you don't want to do this, you can use the old function(){} syntax instead.

$( document ).ready( function() {
    $( ".checkmark" ).on( "click", {self:this}, ( event ) => {
        var checkBoxValue = $(this).data("checkbox1-value")
        alert(checkBoxValue);
    } );
} );

And also as @Erwin mentioned, use only lowercase in your data- attribute name:

<span class="checkmark" id="checkbox1" data-checkbox1-value="-155"></span>

JsFiddle

2 Comments

Thank you so much for your response guys. Love this developer community. :)
@Wasbeer That is great to know <3. Please upvote any helpful answers and select a best answer as well
0

It's returning undefined because it is declared incorrectly. The part after data- should be in lower case. In your case, it must be

<span class="checkmark" id="checkbox1" data-checkbox1-value=-155></span>

for the .data() to work.

Comments

0

this code works for me try it ;)

$( document ).ready( function() {
    $( ".checkmark" ).on( "click",  function()  {
        var checkBoxId = "#" + $(this).attr('id');
          var  checkBoxData =  $(this).attr('id') + "-value";
        $( this ).attr('data-'+ checkBoxData, 155 );
    } );
});

jsfiddle link

Comments

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.