question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting a Attribute without Value

See original GitHub issue

I have a Attribute without a Value wich I’d like to set on a Element:

 element.SetAttribute("readonly", null);

how can I do this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
FlorianRapplcommented, Oct 13, 2016

So answer to the question: There is no attribute “without” a value. Every attribute has a value.

element.SetAttribute("readonly", String.Empty);

is what you actually look for (at least that’s my opinion). The readonly (as an example) only cares about whether its available or not - not what value it has.

1reaction
FlorianRapplcommented, Oct 13, 2016

Yeah, as I said: The behavior comes from the JS to native transition and the cast to a string. We do not have the JavaScript types and our instance is already a (null) string. So writing “null” would not make sense to me. If you plug in a scripting engine then yes - you get the same behavior as expected from JS.

Similarly, using some arbitrary object in JavaScript will result in its string representation (e.g., [object Object] for a general one), but in C# you’ll get a type mismatch exception (if done via reflection; otherwise a compilation error occurs of course). So the exact JS behavior cannot be matched anyway.

Long story short: My aim is not to copy the behavior tied to JS, but the right behavior (i.e., the behavior as defined from the W3C - which is independent of the used language).

Back to the topic: I agree - this is mostly about aesthetics and the serialization of the DOM. So I would say everything is as it should be.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set an Attribute Without a Value in JavaScript - Maker's Aid
The best ways to set an empty attribute to one or multiple DOM elements in your HTML markup, with plain JavaScript.
Read more >
How to set attribute without value using jQuery
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, ...
Read more >
How to declare an attribute in Python without a value
We can directly assign the attributes with the None value. By creating a class and initializing two variables within the class with None....
Read more >
Element: setAttribute() method - Web APIs | MDN
Boolean attributes are considered to be true if they're present on the element at all. You should set value to the empty string...
Read more >
JavaScript : Set attribute without value - YouTube
JavaScript : Set attribute without value [ Gift : Animated Search Engine : https://bit.ly/AnimSearch ] JavaScript : Set attribute without ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found