6

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?

5 Answers 5

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Nice use of dataset!
1

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

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

Comments

1

use setAttribtute()

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

Comments

0

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>

Comments

-1

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

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

2 Comments

I'm really doubting JavaScript will correct parse the -, and will rather try to read them as a subtraction.
You are right, better to use setAttribute() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.