0

I'm currently writing a python HTML parser. When storing attributes and values of tags, I have come across HTML attributes with no assigned value. This is some HTML that I scraped off Github, which is when I encountered the error through testing.

<meta name="request-id" content="someRandomValue" data-pjax-transient>
<span class="js-user-status-original-emoji" hidden></span>

Since I'm storing attributes as keys in a dictionary, I need a value for the key-value pair. What value does an HTML attribute with no assigned value have? What would the values of "hidden" and "data-pjax-transient" be?

asked Dec 26, 2019 at 9:45
1

4 Answers 4

1

It doesn't have a value, it's just a marker or flag. Logically, you can think of it as a boolean value that's true, if the flag is present, or false otherwise.

answered Dec 26, 2019 at 9:49
Sign up to request clarification or add additional context in comments.

3 Comments

So in the span tag, the 'hidden' attribute would be the same as hidden="True".
@user5423, the value does not matter. hidden="True" or hidden="lorem ipsum dolor" means the same thing. As long as the attribute hidden exists (with or without a value), it's interpreted as being true. If hidden does not exist, then it's interpreted as being false.
@user5423, in Python, you could store it as a boolean. If you find an attribute without a value, store the attribute name as the key and the boolean True as the value.
0

Usually if there is now value associated with attribute it is considered true. So in your case <span class="js-user-status-original-emoji" hidden></span>, for attribute hidden, you can store true as value.

answered Dec 26, 2019 at 9:50

Comments

0

HTML Attributes with no assigned values are implicitly given the value of the empty string. However, they are usually used to enable a certain boolean attribute (such as disabling a button). For example,

if (element.dataset.disabled !== undefined) {
 // Prevent user from clicking button
}
answered Dec 26, 2019 at 9:51

Comments

0

Simply use None in the dictionary as a value and put an if condition in the Python script.

answered Dec 26, 2019 at 9:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.