I am trying to set the data-id and/or value of a span from my js file after a click event.
<span id="test"></span>
my sudo code js file
nextLink: function(event) {
$('#test').val = 3;
$('#test').data('id') = 'Next';
},
The data-reactid attribute is a custom attribute that react can easily identify its components within the DOM. Just like the HTML “classes” and “id” attributes, “data-reactid” helps in uniquely identifying the element .
Use the querySelector method to get an element by data attribute, e.g. document. querySelector('[data-id="box1"]') . The querySelector method returns the first element that matches the provided selector or null if no element matches the selector in the document.
You can simply use the jQuery attr() method to find the data-id attribute of an HTML element.
Try setting it as an attribute..
$('#test').attr('data-id' , 'Next'); // JQuery
You can also try the setAttribute()
..
var d = document.getElementById("test"); // Javascript
d.setAttribute('data-id' , 'Next'); //
Using this approach will reflect the new attribute in the DOM
In your code You are trying to set the attribute which is not present .. So you need to add the attribute first to add that ..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With