Rip a Document to Pieces

Share This Post

More often than not, you do not program in a vacuum. That is true not only of the process and a team of people but with code itself. What I mean is that you typically do not simply build JavaScript (or some other code language) applications by themselves, but, rather, JavaScript interacts with HTML and CSS. HTML pages (or our content) are documents of text that are structured into pieces. When someone creates an HTML page, they create a hierarchical structure…head, body, header, section, nav, footer and more. The functionality of a web app is provided by JavaScript (typically). So, in order to have high quality, interactive, robust applications, JavaScript has to be able to work within and manipulate the HTML document. This is done utilizing what is known as the Document Object Model (DOM). JavaScript has built in functions/methods that allow a developer to pretty much do whatever they would like with an HTML document. Today I want to show one example…grabbing an element or tag by their ID.

Whenever I create an HTML document, I utilize the ID and class elements extensively. This is key for styling a page and for the interactivity. Let me show you how you can use the ID of an element to manipulate the DOM. First, let’s talk about IDs. The syntax for an ID is as follows…

[code lang=”js”]
<!– A typical HTML tag..in this case a form input tag. –>
<input type = "text" id = "someID" name = "someName">
<!– This code would result in a form text box being output to the HTML document. –>
[/code]

Notice that I have the ID attribute with the value of “someID”. One thing to note about IDs is that they can only exist one time within any HTML document. What that means is that there can only be one element with the ID of “someID”. I cannot create another element and re-use that same ID. This is to our advantage, because we can use JavaScript to grab that element, in this case, the input tag.

So how do we do that? We use a function/method entitled, “getElementById()”. Here is an example…

[code lang=”js”]
// Create a variable to hold the information from our input tag element
var myVar;
// Grab the value of the input tag field and store it in myVar
myVar = document.getElementById("someID").value;
[/code]

So the way this works is we tell the JavaScript interpreter to go into the document, get an element by the ID of someID and then grab the value of that element. Since this is a form field, what that means is we can grab whatever information the end user typed into the form text field. We can take that information and store it in a variable like I have done here and then do with it what we would like. FYI, in this case I am grabbing the element’s value, but you do not have to do that. You can simply grab the entire element without the “.value” on the end.

Happy Coding!

Clay Hess

More To Explore

Code

Demystifying Scrum User Stories Confirmation: Ensuring Quality and Collaboration

One of the key elements of Scrum is the use of user stories to define the requirements of a system or feature from the perspective of the end user. As teams work through the product backlog, it becomes crucial to confirm the user stories to ensure they meet the desired criteria and are ready for development. In this blog post, we’ll explore the concept of Scrum user stories confirmation and its significance in delivering high-quality products.

Code

The Power of Conversations in Scrum User Stories

At the heart of Scrum lies the concept of user stories, which serve as a means of capturing requirements from the perspective of end-users. However, the true power of user stories lies not just in their written form but also in the conversations that take place around them. In this blog post, we will explore the significance of conversations in Scrum user stories and how they contribute to the success of Agile projects.

Do You Want To Boost Your Business?

drop us a line and keep in touch

Scroll to Top
small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.