admin管理员组

文章数量:1291024

I have the following code.

<li class="source" data-toggle="tooltip" data-placement="top" id="content1" title="" data-original-title="Test">Test Server 1</li>

I'm trying to change the content of data-original-title using the following code:

document.getElementById('content1').style["data-original-title"] = 'Online';

Am I doing something wrong?

I have the following code.

<li class="source" data-toggle="tooltip" data-placement="top" id="content1" title="" data-original-title="Test">Test Server 1</li>

I'm trying to change the content of data-original-title using the following code:

document.getElementById('content1').style["data-original-title"] = 'Online';

Am I doing something wrong?

Share Improve this question asked Nov 9, 2015 at 19:16 Dr. BananaDr. Banana 4351 gold badge7 silver badges19 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

You can use .dataset

document.getElementById('content1').dataset.originalTitle = 'Online';
<li class="source" data-toggle="tooltip" data-placement="top" id="content1" title="" data-original-title="Test">Test Server 1</li>

or .setAttribute

document.getElementById('content1').setAttribute('data-original-title', 'Online');
<li class="source" data-toggle="tooltip" data-placement="top" id="content1" title="" data-original-title="Test">Test Server 1</li>

data-original-title is an attribute, so you would need to set it as such:

document.getElementById('content1').setAttribute('data-original-title','Online');

use setAttribtute()

document.getElementById("content1").setAttribute("data-original-title", "Online");

Try this one:

document.getElementById('content1').dataset.originalTitle = 'Online';
<li class="source" data-toggle="tooltip" data-placement="top" id="content1" title="" data-original-title="Test">Test Server 1</li>

This does not seem to be a style, try this:

document.getElementById('content1').data-original-title = 'Online';

本文标签: change html attribute with javascriptStack Overflow