taebu
taebu

Reputation: 57

how to replace data("id") in jquery

i had nestable but i want to change

data-id="12" 

to

may be Max data-id equals 17

and than I want to the change the data-id value

data-id="39"

Here is code:

<!--before-->
<div class="dd" id="nestable">
<ol class="dd-list" id="depth1_ol">
<li data-id="12"><div class="dd-item">Item 12</div></li>
</ol>
</div>

<!--after-->
<div class="dd" id="nestable">
<ol class="dd-list" id="depth1_ol">
<li data-id="39"><div class="dd-item">Item 39....</div></li>
</ol>
</div>

how can do this??

Upvotes: 0

Views: 3215

Answers (2)

Leo
Leo

Reputation: 14820

Your question is a bit a unclear but I'm assuming that you need to change the data-id attribute. Well, to change the value using jQuery you can use the data function like below...

$("li").data("id", "39");

however, be aware that this will not change the actual DOM element, it will be stored in memory for further manipulation within the jQuery framework. If you want to change the DOM directly, then use the attr function as below...

$("li").attr("data-id", "39"); 

Upvotes: 4

Sudharsan S
Sudharsan S

Reputation: 15393

Actually data attribute is not visible in html.The .data() method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks.

$("div#nestable ol li").data("id" , 39);
$("div#nestable ol li").find(".dd-item").text("Item 39");

Upvotes: 1

Related Questions