Chapter 3 · Basics
Changing content
Now that you can find elements, it's time to change them. In this chapter you'll learn how to read and change text, form fields and attributes, and how to add and remove elements on the page.
One rule: read or change
Most methods in this chapter work in two ways, and the brackets tell Nyx which one you want:
Empty brackets: read
nyx("#name").text()Gives you the text that's there now.
Something inside: change
nyx("#name").text("Asha")Replaces the text with Asha.
Remember this once and it works for .text(), .html(), .value(), .attr() and .data().
Changing text
.text() reads or changes the words inside an element. It's the method you'll use most.
Welcome, guest
You have 0 new messages.
The first line shows the old text in the Output. The next two lines change what's on the page.
If you write .text("<b>Hi</b>"), the page shows those characters, angle brackets and all. It never turns them into bold text. That makes .text() safe for anything a visitor typed.
Changing HTML
When you want tags to work, like bold text or a link, use .html() instead.
Homework is due on Friday.
Change .html to .text and run it again. See the difference?
If someone types HTML into a box on your page and you pass it to .html(), their HTML runs on your page. It can even run code. Use .html() only with HTML you wrote. For anything a visitor typed, use .text().
Form fields
Text boxes, dropdowns and checkboxes don't have text inside them in the usual way. To read or fill them, use .value().
<input type="button">Its value attribute, which is also the words on the button.<input type="checkbox">true if it's ticked, false if not.<input type="color">The chosen colour as a hex code: "#ff8800".<input type="date">The date as text, year first: "2026-11-29".<input type="datetime-local">Date and time as text, joined by a T: "2026-11-29T14:30".<input type="email">What's typed, as text: "asha@example.com".<input type="file">The chosen file, or null if none. With multiple, a list of files. See below.<input type="hidden">Its value attribute. Visitors can't see it, but your code can read and change it.<input type="image">Its value attribute, usually empty. It's a picture used as a submit button.<input type="month">Year and month as text: "2026-11".<input type="number">The number, but as text: "42". See the note below.<input type="password">What's typed, as text, even though the page shows dots.<input type="radio">Ask for the chosen one in the group: nyx("[name=size]:checked").value(). Without :checked you get the first radio's value, even if it isn't chosen.<input type="range">Where the slider is, as text: "75".<input type="reset">Its value attribute, the words on the button.<input type="search">What's typed, as text.<input type="submit">Its value attribute, the words on the button.<input type="tel">What's typed, as text: "+91 98300 12345". It stays text, so a leading 0 or + is kept.<input type="text">What's typed, as text. A plain <input> with no type works the same way.<input type="time">The time as text, 24-hour clock: "14:30".<input type="url">What's typed, as text: "https://example.com".<input type="week">Year and week number as text: "2026-W48".<textarea>Everything typed in the big box, as text, including new lines.<select>The value of the chosen <option>.<select multiple>A list (an array) of every chosen value: ["maths", "art"].An empty box gives empty text: "". Date and time boxes always give the same year-first format, whatever the visitor's computer shows on screen.
Even from a number box, .value() gives you text like "42". Before doing maths, turn it into a real number with Number(): Number(nyx("#age").value()) + 1 gives 43. Without it, "42" + 1 gives "421".
Press Run first. Then type your name in the box and click Say hello:
…
const name = nyx("#c-name").value();Read what's typed in the box and keep it in a variable calledname. A variable is a labelled box for a value, so you can use it on the next line."Hello, " + name + "!"The+joins pieces of text together. IfnameisAsha, this becomesHello, Asha!.
To fill a field, put the value inside the brackets: nyx("#c-name").value("Ravi"). To empty it, give it empty quotes: .value(""). For a checkbox, use .value(true) to tick it and .value(false) to untick it.
File boxes
An <input type="file"> lets a visitor choose a file from their computer. .value() gives you the chosen file itself, with useful facts about it:
file.nameThe file's name: "homework.pdf".file.sizeIts size in bytes. Divide by 1024 for kilobytes (KB).file.typeWhat kind of file it is: "image/png", "application/pdf".Press Run, choose any file, then click Check file:
Nothing is uploaded: the file stays on your computer. Sending it to a server is covered in Servers and AJAX.
For safety, browsers only let the visitor pick files. .value("") clears the choice, but you can't put a file in the box with code. If you try, Nyx explains why in the console. Add multiple to the tag to let visitors pick several files; .value() then gives you a list of them.
Attributes
Attributes are the settings written inside a tag: the src of an image, the href of a link, the placeholder of a box. Read or change any of them with .attr().
.attr(Change an attribute
"href"Which one
, "/home"Its new value
)Done
Changing several at once, and removing
// Several at once: put them in { } curly brackets
nyx("#c-photo").attr({ src: "asha.jpg", alt: "Asha smiling" });
// Remove an attribute
nyx("#c-link").removeAttr("target");To disable a button so it can't be clicked, use .prop("disabled", true). Use false to switch it back on. You'll use this a lot with forms.
Storing data on an element
You can make up your own attributes, as long as they start with data-. They're a handy place to keep extra facts about an element, like a price or an id, without showing them on the page.
<li id="c-pen" data-price="20" data-stock="15">Gel pen</li>Read them with .data(), using the name after data-:
- Gel pen
Nyx turns "20" into the number 20 for you, so maths like price * 3 just works. (* means multiply.)
Adding new elements
Four methods add new content. The only difference is where it goes, compared with the element you found:
<!-- .before() puts it here -->
<ul id="list">
<!-- .prepend() puts it here -->
<li>Maths</li>
<li>Science</li>
<!-- .append() puts it here -->
</ul>
<!-- .after() puts it here -->.append(...)Inside, at the end. The one you'll use most, for example to add an item to a list..prepend(...)Inside, at the start. Good for "newest first" lists..before(...)Outside, just before it..after(...)Outside, just after it.Subjects:
- Maths
- Science
Try .before("<p>New term!</p>") or .after(...) on #c-subjects too.
Building elements safely
Writing HTML in a string, like "<li>English</li>", is fine when you typed it yourself. But when the text comes from a visitor, build the element with nyx.create() instead. It always treats the text as plain text, so it's always safe.
nyx.create("li", { text: "English" })nyx.create(Make a brand-new element. It isn't on the page yet."li"What kind of element: here, a list item.{ text: "English" }Its settings, in curly brackets.textis what goes inside it. Anything else, likeclass: "done", becomes an attribute.
Then add it to the page with .append() or any of the other three:
- Ravi: Is the test tomorrow?
The <b> tags show up as plain characters, so a visitor can't sneak HTML onto your page.
Removing elements
.remove()Take the element off the page completely..empty()Keep the element but delete everything inside it..replaceWith(...)Swap the element for something new..clone()Make a copy, which you can then add somewhere with .append().- Read chapter 1
- Read chapter 2
- Read chapter 3
The banner is gone completely. The list was emptied but is still there, so we could add a new item to it.
Putting it together
Here's everything from this chapter working together: a small to-do list. Press Run, then type a task and click Add.
- Buy a notebook
const task = nyx("#c-todo").value();Read what's typed in the box.if (task === "") return;If the box is empty, stop here.===means "is exactly equal to", andreturnmeans "stop".nyx("#c-todos").append(nyx.create("li", { text: task }));Build a new list item with the task as its text and add it to the end of the list.nyx("#c-todo").value("");Empty the box, ready for the next task.
Common mistakes
nyx("#name").textForgot the brackets, so nothing happens. Methods always need (): .text().nyx("#box").text() on an inputText boxes have no text inside them. Use .value() to read what's typed..html(whatTheyTyped)Unsafe. Use .text(), or nyx.create("li", { text: ... })..attr("src") expecting a changeWith one value, .attr() only reads. Give it two: .attr("src", "new.jpg")..data("data-price")Leave out data-. Write .data("price").Good to know
.val()jQuery's name for .value(). It works, but Nyx reminds you to write .value(). See Coming from jQuery..text(old => old + "!")Give .text() a function to change the existing text instead of replacing it.nyx("<li>New</li>")Another way to make an element from HTML you wrote yourself.nyx.escape(text)Makes a visitor's text safe to put inside an HTML string.Exercise
Fill in the blanks, then press Check answers.
1. Change the text of #title to Results.
nyx("#title").("Results");
2. Read what's typed in the box #email.
const email = nyx("#email").();
3. Change the src of the image #photo to cat.jpg.
nyx("#photo").attr("", "cat.jpg");
4. Add a new item to the end of the list #menu.
nyx("#menu").("<li>Contact</li>");
5. Take #popup off the page.
nyx("#popup").();
6. Safely build a list item whose text is what the visitor typed.
nyx.("li", { text: typed });
Remember
.text() for words, .html() only for HTML you wrote yourself..value() reads and fills form fields..attr("name", "value") changes any attribute. .data("name") reads data- attributes..append() adds inside at the end, .prepend() at the start, .before() and .after() outside.nyx.create() for anything a visitor typed..remove() deletes an element. .empty() deletes what's inside it.