JavaScript?: DOM Manipulation

JavaScript: DOM Manipulation

In this article, I will explain how to do DOM manipulation using pure JavaScript. We have many other libraries available to do this work but I want to show you, how to do it in pure JavaScript.

For this, I am taking a simple example of adding input text inside a dropdown on a button click. Final example will look like this:


Now, let's have an HTML file and JS file. In this HTML file, I have taken 3 components,

  1. Input text to enter text
  2. Select component
  3. Button
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Test</title>
  </head>
  <body>
    <div>
      <form>
        <div class="form-group">
          <label>Enter text to be added:</label>
          <input id="inputText" type="text" placeholder="enter text..." />
        </div>
        <br />
        <div class="form-group">
          <label>Dropdown:</label>
          <select id="dropdown"></select>
        </div>
        <br />
        <button id="submit" type="submit">add</button>
      </form>
    </div>


    <script src="main.js"></script>
  </body>
</html>

The HTML looks like this:

No alt text provided for this image



Now, let's write a javascript code. Let's define a file main.js and add in a script tag (as defined earlier).

Search button so that we can add a 'click' event with it. I am using the getElementById method for this. It will search for a component whose "id" is "submit". "id" is a field unique id defined in the HTML mentioned above.

const addButton = document.getElementById("submit");
addButton.addEventListener('click', handleClick)

'handleClick' is the callback function which will be called whenever button is clicked. In this function, we going to do the following tasks:

  1. Extract data from the input text field. Similar to the button, I search component by their id's.
const inputText = document.getElementById("inputText");
const select = document.getElementById("dropdown");

2. Create an 'option' field and add entered text to it

const selectFieldOption = document.createElement("option");
selectFieldOption.value = inputText.value;
selectFieldOption.append(inputText.value);

3. Attach this field with the 'select' component

select.appendChild(selectFieldOption);

Now, you have attached your 'option' field with the 'select' component. Now, whenever you add new text and click on the button, a new field will be added to the select component.

The complete code is as below:

// search button by id
const addButton = document.getElementById("submit");


// adding click event with the button
addButton.addEventListener('click', handleClick)


function handleClick(e) {
  e.preventDefault();


  const inputText = document.getElementById("inputText");
  const select = document.getElementById("dropdown");


  //to avoid blank value
  if (inputText.value) {
    const selectFieldOption = document.createElement("option");
    selectFieldOption.value = inputText.value;


    selectFieldOption.append(inputText.value);


    //to clear input
    inputText.value = "";
    select.appendChild(selectFieldOption);
  }
}

That's for this article. Kindly let me know if you have any questions.


要查看或添加评论,请登录

Tushar Goel的更多文章

  • DB Isolation Levels Part -2 (Repeatable Read and Serializable)

    DB Isolation Levels Part -2 (Repeatable Read and Serializable)

    In the last article, we learned about Read un-committed and Read committed isolation levels and the problems linked…

  • DB Isolation Levels in Detail - Part -1

    DB Isolation Levels in Detail - Part -1

    If two transactions don't touch the same data, they can safely run in parallel because they are not dependent on each…

    1 条评论
  • How Kafka is so efficient?

    How Kafka is so efficient?

    What is Apache Kafka? Apache Kafka is a distributed streaming platform that allows you to:: Publish and subscribe to…

  • Dependency Inversion

    Dependency Inversion

    Dependency ingestion is ‘D’ in the SOLID design principle. It is a guideline that helps design loosely coupled classes.

  • Law of Demeter (Principle of least knowledge)

    Law of Demeter (Principle of least knowledge)

    In the last article we have discussed Tell, Don’t ask guideline. In this article, we will discuss another guideline…

  • Tell, Don't ask!

    Tell, Don't ask!

    Few days back I was having a discussion with one of my friends and we were discussing how to reduce coupling between…

  • Concurrency Pattern: Active object pattern

    Concurrency Pattern: Active object pattern

    This pattern is a type of concurrency design pattern and widely used in applications. Also, it is used to create…

  • The JavaScript EventLoop

    The JavaScript EventLoop

    This post will explain one of the most important concepts of JavaScript i.e.

  • How to use an index in MongoDB?

    How to use an index in MongoDB?

    In this post, I will explain how to use an index in MongoDB. In an earlier article, I explained how indexes work.

    3 条评论

社区洞察

其他会员也浏览了