Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

jquery set data attribute value

//<div id="myElementID" data-myvalue="37"></div>
var a = $('#myElementID').data('myvalue'); //get myvalue
$('#myElementID').data('myvalue',38); //set myvalue
Comment

set data attribute with a string jquery

alert($('#outer').html());   // alerts <div id="mydiv" data-myval="10"> </div>
var a = $('#mydiv').data('myval'); //getter
$('#mydiv').attr("data-myval","20"); //setter
alert($('#outer').html());   //alerts <div id="mydiv" data-myval="20"> </div>
Comment

jquery data attribute

/* html */
<a data-number="123">link</a>

/* js */
$(this).attr("data-number") // returns string "123"

$(this).data("number") // returns number 123 (jQuery >= 1.4.3 only)
Comment

set custom data attribute jquery

//set the custom data value
let mainDiv = $('#yourDivId').data('anyName', value);
//access the custom data value
mainDiv.data('anyName');
Comment

change data attribute jquery

//sometimes it is better to use JS
<script>
// 'Getting' data-attributes using getAttribute
var plant = document.getElementById('strawberry-plant');
var fruitCount = plant.getAttribute('data-fruit'); // fruitCount = '12'

// 'Setting' data-attributes using setAttribute
plant.setAttribute('data-fruit','7'); // Pesky birds
</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: js selection box excel node 
Javascript :: document get element by id radio button 
Javascript :: javascript blur focus active element 
Javascript :: committing parts of a file git 
Javascript :: random id number nodejs 
Javascript :: express get query params from url 
Javascript :: electron getPath 
Javascript :: JS DOM how to add a class name to any HTML element 
Javascript :: jest console.log 
Javascript :: select add option js 
Javascript :: get pods running on a node 
Javascript :: difference between devDependency and dependency 
Javascript :: how to use more than one transform in javascript 
Javascript :: vs code is showing 5k untracked files when nothing is changed from last commit 
Javascript :: array methods javascript 
Javascript :: javascript last element of an array 
Javascript :: javascript clear interval 
Javascript :: PG::DuplicateTable: ERROR: relation already exists 
Javascript :: javascript remove all event listeners 
Javascript :: nestjs return error response 
Javascript :: spring rest api cors error in react app 
Javascript :: encode in javascript 
Javascript :: javascript check if number is a power of 2 
Javascript :: how to copy to clipboard in react js 
Javascript :: HashLocationStrategy 
Javascript :: datatable hide columns 
Javascript :: adding firebase to angular 
Javascript :: how to convert to one decimal place javascript 
Javascript :: wait for element to load 
Javascript :: debug react vscode 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =