Can't update data-attribute value

后端 未结 9 1540
北海茫月
北海茫月 2020-11-27 17:11

Although there are some examples about this on the web, it does not seem to work correctly. I can\'t figure out the problem.

I have this simple html

         


        
9条回答
  •  暖寄归人
    2020-11-27 17:33

    This answer is for those seeking to just change the value of a data-attribute

    The suggested will not change the value of your Jquery data-attr correctly as @adeneo has stated. For some reason though, I'm not seeing him (or any others) post the correct method for those seeking to update their data-attr. The answer that @Lucas Willems has posted may be the answer to problem Brian Tompsett - 汤莱恩 is having, but it's not the answer to the inquiry that may be bringing other users here.

    Quick answer in regards to original inquiry statement

    -To update data-attr

    $('#ElementId').attr('data-attributeTitle',newAttributeValue);
    

    Easy mistakes* - there must be "data-" at the beginning of your attribute you're looking to change the value of.

提交回复
热议问题