Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
42
rated 0 times [  45] [ 3]  / answers: 1 / hits: 29446  / 12 Years ago, wed, october 24, 2012, 12:00:00

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';
},

More From » javascript

 Answers
17

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 ..


[#82373] Tuesday, October 23, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stacie

Total Points: 476
Total Questions: 92
Total Answers: 102

Location: Bosnia and Herzegovina
Member since Tue, Mar 29, 2022
2 Years ago
stacie questions
Fri, Jun 26, 20, 00:00, 4 Years ago
Thu, Jan 23, 20, 00:00, 4 Years ago
Fri, Aug 30, 19, 00:00, 5 Years ago
Fri, Aug 2, 19, 00:00, 5 Years ago
;