Frequently asked questions

Syntax, keywords and built-in functions

Standard distribution

import implementation

Brython packages

Browser interface

Brython-specific built-in modules

Working with Brython



Accessing elements

Getting access to an element can be done in different ways. The most usual is to use its identifier, ie its attribute id : with an input field defined by

<input id="data">

we can get a reference to this field by

from browser import document
data = document["data"]

document is defined in module browser and refers to the HTML document. It behaves like a dictionary whose keys are the identifiers of the elements in the page. If no element has the specified id, the program raises a KeyError exception.

All the elements in the page have a method get() that can be used to search elements:

  • elt.get(name=N) returns a list of all the elements descending from elt whose attribute name is equal to N
  • elt.get(selector=S) returns a list with all the elements descending from elt whose CSS selector matches S is an alias for elt.get(selector=S). A few examples :'.foo')       # elements with class "foo"'form')       # list of tags "<form>"'')     # H1 tags with class "bar"'#container') # the element with id "container", same as
                              # [document["container"]]'a[title]')   # A tags with an attribute "title"