0

I am trying to access the title of an html div element which is on the page, from a browser extension. When I console log the element in firefox and view it in the dev tools, I get this output:

<div class="X" A="Y" B="Z" C="XX">

When I click the on the above to expand it, I get the list of element characteristics:

...
tagName: "DIV"
textContent: "what is in the text"
title: "what I want to get"

Yet, when I console log element.title I get an empty string. I get undefined when I do element.innerHTML, when there is clearly something there.

2
  • 1
    The element you've mentioned does not have title attribute Commented Jul 11, 2019 at 4:46
  • When I click on it, I get the expanded list of attributes which includes the title. Commented Jul 11, 2019 at 15:10

2 Answers 2

1

If the element does not have a title attribute it won't log anything. I assume you might be seeing a different div, consider the following:

console.log("class X div title: ", document.getElementsByClassName('X')[0].title);
console.log("class F div title: ", document.getElementsByClassName('F')[0].title);
//jQuery
console.log("class X div title: ", $(".X").prop('title'));
console.log("class F div title: ", $(".F").prop('title'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="X" A="Y" B="Z" C="XX">innerHTML for X</div>
<div class="F" A="Y" B="Z" C="XX" title="target title">innerHTML for F</div>

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

2 Comments

I am certain I am seeing the correct div. What I am doing is: console.log(element); console.log(element.title);
@JohnAbelDoe could you try out the same in another browser, say chrome? Does it have the same behavior?
0

If you want to access to the title of an htmlElement, it must be set on, on your example it haven't, take a look on mine :

<div class="X" A="Y" B="Z" C="XX" style="styleX" title="myAwesomeTitle">

Comments

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.