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.

There is no attribute "id" in any tag definition

See original GitHub issue

Actual workaround :

div(.....) {
   attributes["id"] = "special-div"
}

Expected api :

div(id = "special-div".....) {}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:8
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
fatjoemcommented, Jul 13, 2018

I am currently converting the templates for a large website to kotlinx.html. Those templates are using id and also class attributes a lot. Being able to add classes as a parameter but not the id, feels very inconsistent and unsexy.

In a new project I would try to minimize using id attributes, hence this will not be a big issue. But for converting existing projects, it is a big pain.

1reaction
Tatskaaricommented, Mar 13, 2018

Hi, just had a similar confusion and came up with this helper function for divs which allows specifying arbitrary attributes:


import org.testng.annotations.Test
import kotlinx.html.*
import kotlinx.html.stream.appendHTML
import java.io.StringWriter
import kotlin.test.assertEquals


object HTMLTest {
  fun FlowContent.div(classes: String = "", vararg attributes: Pair<String, String>, block: FlowContent.() -> Unit) {
    val attributeMap = attributes.toList()
      .plus("class" to classes)
      .associate { it }
    DIV(attributeMap, consumer).visit(block)
  }
  
  

  @Test
  fun `test html with attribute list`(){
    val stringWriter = StringWriter()
    stringWriter.appendHTML().html {
      body {
        div ("main", "id" to "main-body", "style" to "float: left") {
          text("main")
        }
      }
    }
    assertEquals("""
<html>
  <body>
    <div id="main-body" style="float: left" class="main">main</div>
  </body>
</html>""".trim(), stringWriter.toString().trim())
  }
}

What do you think? I find the pattern a bit more intuitive (especially for people less familiar with Kotlin) than accessing fields of the tag object via extension method magic.

Read more comments on GitHub >

github_iconTop Results From Across the Web

flask-login using UserMixin - no 'id' attribute error despite ...
The reason we are getting this specific error is because by calling User.get_id(id) , you are calling get_id with self=id and since id...
Read more >
id - HTML: HyperText Markup Language - MDN Web Docs
The id global attribute defines an identifier (ID) which must be unique in the whole document. Its purpose is to identify the element...
Read more >
HTML - The id attribute - W3Schools
The HTML id attribute is used to specify a unique id for an HTML element. You cannot have more than one element with...
Read more >
'NoneType' object has no attribute 'id' - asdeasoft.net
AttributeError means that there was an Error that had to do with an Attribute request. In general, when you write x.y, y is...
Read more >
Links in HTML documents - W3C
The id and name attributes share the same name space. This means that they cannot both define an anchor with the same name...
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