I'm having a problem of assigning a value to an anchor tag's data-attribute. Here is my code:
<script>
window.onload = function(){
document.getElementById("setcolor").click();
}
var color = "red";
document.getElementById("mycolor").value = color;
</script>
<a id="setcolor" class="colors" href="#" role="button" data-value="mycolor">Choose color</a>
I would like to set the following string value to the href above (replace 'mycolor' with 'red'):
data-value="red"
but the above is not working. Any tips are appreciated.
你试试:
document.getElementById("setcolor").setAttribute("data-value", color);
Try rewriting your code:
<script>
window.onload = function(){
document.getElementById("setcolor").click();
}
var color = "red";
var setcolor = document.getElementById("setcolor");
setcolor.dataset.value = color; //sets data-value="red"
</script>
sample here https://jsfiddle.net/fjchy6av/1/
Simply update your code to,
<script>
window.onload = function(){
var color = "red",
setcolorLink = document.getElementById("setcolor");
setcolorLink.setAttribute("data-value", color);
setcolorLink.click();
}
</script>
<a id="setcolor" class="colors" href="javascript:void(0)" role="button" data-value="mycolor">Choose color</a>
Example: https://jsfiddle.net/1usopvda/2/
There are 2 ways of doing this. See the examples below, how to use data-attribute
in JavaScript.
var colorLink = document.getElementById("setcolor");
Using DOM's getAttribute()
property
var getColor = colorLink.getAttribute("data-value") //returns "mycolor" colorLink.setAttribute("data-value", "red") //changes "data-value" to "red" colorLink.removeAttribute("data-value") //removes "data-value" attribute entirely
Using JavaScript's dataset
property
var getColor = colorLink.dataset.value //returns "mycolor" colorLink.dataset.value = 'red' //changes "data-value" to "red" colorLink.dataset.value = null //removes "data-value" attribute
click()
in the question. So if you want to change the value onclick, see the example below <script>
window.onload = function(){
var color = "red",
setcolorLink = document.getElementById("setcolor");
setcolorLink.onclick = function(){
this.setAttribute("data-value", color);
};
}
</script>
<a id="setcolor" class="colors" href="javascript:void(0)" role="button" data-value="mycolor">Choose color</a>
Example: https://jsfiddle.net/1usopvda/4/
Your js usage is not correct. Try it out following ```
<script>
window.onload = function(){
document.getElementById("setcolor").click();
}
var color = "red";
document.getElementById("setcolor").setAttribute("mycolor", color);
</script>
<a id="setcolor" class="colors" href="#" role="button" data-value="mycolor">Choose color</a>
```
If you need only to change the color on click for you <a>
tag you can consider a much more straightforward solution using only CSS, in this case a CSS :active Selector .
#setcolor:active{ color: red; }
<a id="setcolor" class="colors">Choose color (Click me!)</a>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.