3

I want to make the value of "data-hello" 2

 $('# Indexdiv').data('hello', 2); // not working, why?   
 $('#indexDiv').attr('data-hello',2); //is working. 

why data(key, value) is not working?

enter image description here

    <!DOCTYPE html>
    <html>
    <head>
        <title>Page Title</title>
    
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
        <div id="indexDiv" data-hello=1>
            Hello world
        </div>
        <button id="btnClick">change id attribute</button>
    </body>
    
    
    <script>
        $(function() {
            $('#btnClick').click(function(){
                $('#indexDiv').data('hello',2);  //not working, why???
               // $('#indexDiv').attr('data-hello',2); //is working
            });
         });
    </script>
    </html>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415

0 Answers0