How about inserting a fresh div block with jQuery?

I am trying to dynamically insert a new div block when the insert button is clicked. I found some resources online but they all use a constant div block structure. I want to be able to call the new div by its id and also have an option to remove it on click. Instead of hardcoding "

" in my code, I want to be able to reference it using an id. Can anyone guide me on how to achieve this?

Any help would be greatly appreciated.

$('.add').click(function() {
    $('.block:last').before('<div class="main-block"><div class="main-block"></div><span class="remove">Remove Option</span></div>');
});
$('.main-block').on('click','.remove',function() {
  $(this).parent().remove();
});
.block {
  display: block;
}

span {
  display: inline-block;
  cursor: pointer;
  text-decoration: underline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main-block">
  <div class="col-md-2 main-title">
    <b>Language</b>
    <label class="title">Japan </label><br>
    <label class="title">Chinese </label><br>
    <label class="title">English </label><br>
    <label class="title">Korea </label>
  </div>
  <div class="col-md-4 main-title">
    <b style="margin-left:40px;">Name</b>
    <select class="selectbox" name="select1" id="select1">
      <option value="1">Business Center</option>
      <option value="2">Option 1</option>
      <option value="3">Option 2</option>
      <option value="4">Option 3</option>
      <option value="5">Option 4</option>
    </select>
    <select class="selectbox" name="select2" id="select2">
      <option value="1">Chinese</option>
      <option value="2">Option 1</option>
      <option value="3">Option 2</option>
      <option value="4">Option 3</option>
      <option value="5">Option 4</option>
    </select>
    <select class="selectbox" name="select3" id="select3">
      <option value="1">English Center</option>
      <option value="2">Option 1</option>
      <option value="3">Option 2</option>
      <option value="4">Option 3</option>
      <option value="5">Option 4</option>
    </select>
    <select class="selectbox" name="select4" id="select4">
      <option value="1">Korea Center</option>
      <option value="2">Option 1</option>
      <option value="3">Option 2</option>
      <option value="4">Option 3</option>
      <option value="5">Option 4</option>
    </select>
  </div>
  <div class="col-md-6 main-title">
   <b style="margin-left:40px;">Alias Name</b>
    <input type="text" class="form-control text-box" />
    <input type="text" class="form-control text-box" />
    <input type="text" class="form-control text-box" />
    <input type="text" class="form-control text-box" />
  </div>

  <div class="block">
    <span class="add">Add Option</span>
  </div>
</div>

Answer №1

Check out the following code snippet

$(function(){
  $('.add').click(function() {
     $("#main-block").show();
  });
$('.remove').on('click',function() {
  $("#main-block").hide();
});
});
.block {
  display: block;
}

span {
  display: inline-block;
  cursor: pointer;
  text-decoration: underline;
}

#main-block {
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main-block">
  <div class="col-md-2  main-title">
    <b>Language</b>
    <label class="title">Japanese </label><br>
    <label class="title">Chinese </label><br>
    <label class="title">English </label><br>
    <label class="title">Korean </label>
  </div>

  <div class="col-md-4  main-title">
    <b style="margin-left:40px;">Name</b>
    <select class="selectbox" name="select1" id="select1">
      <option value="1">Business Center</option>
      <option value="2">Option 1</option>
      <option value="3">Option 2</option>
      <option value="4">Option 3</option>
      <option value="5">Option 4</option>
    </select>

    <select class="selectbox" name="select2" id="select2">
      <option value="1">Chinese</option>
      <option value="2">Option 1</option>
      <option value="3">Option 2</option>
      <option value="4">Option 3</option>
      <option value="5">Option 4</option>
    </select>

    <select class="selectbox" name="select3" id="select3">
      <option value="1">English Center</option>
      <option value="2">Option 1</option>
      <option value="3">Option 2</option>
      <option value="4">Option 3</option>
      <option value="5">Option 4</option>
    </select>

    <select class="selectbox" name="select4" id="select4">
      <option value="1">Korea Center</option>
      <option value="2">Option 1</option>
      <option value="3">Option 2</option>
      <option value="4">Option 3</option>
      <option value="5">Option 4</option>
    </select>
  </div>

  <div class="col-md-6 main-title">
   <b style="margin-left:40px;">Alias Name</b>
    <input type="text" class="form-control text-box" />
    <input type="text" class="form-control text-box" />
    <input type="text" class="form-control text-box" />
    <input type="text" class="form-control text-box" />
  </div>
  
  <div class="main-block" id="main-block"><div class="main-block"></div><span class="remove">Remove Option</span></div>
  
  <div class="block">
    <span class="add">Add Option</span>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

TinyMCE toolbar missing the "hr" option

I am encountering an issue while using TinyMCE as my editor. I have added the plugin as instructed, but I cannot find the "hr" button/option in the editor interface. If anyone has any insights or solutions to this problem, please share! This is how I am ...

Using Jquery to bind events for selecting focus in and out

There seems to be an issue with binding jQuery events to a select element. The desired behavior is for the .focus class to be added when the select is clicked. However, the logic breaks when an option is selected from the dropdown. See it in action here: ...

Unable to pinpoint the source of the excess spacing within a form field

There appears to be some extra space in the text field of my form, extending beyond the container margin. Please refer to the image and JSFiddle http://jsfiddle.net/GRFX4/. Any thoughts on what might be causing this issue? Thank you. HTML: <div id="co ...

If the iframe's CSS source is updated, the parent's CSS source will also change

I'm currently working on a unique school project that involves creating multiple CSS styles for different views. <link rel="stylesheet" type="text/css" href="css/main.css" title="main" media="screen"> <link rel="stylesheet" type="text/css" h ...

Tips for populating selectsize JS automatically while editing the HTML form

I am currently working on a form that consists of 3 fields: 2 input text fields and 1 select dropdown menu. After adding data to the form, I encountered an issue when trying to edit the information. While the input text fields retain the old data as expe ...

Is there a way to adjust the size of the canvas element in HTML without compromising quality or resorting to zooming?

I'm currently working on a basic modeling application for the web. The main component of my app is a canvas element that I'm trying to size correctly. However, when I set the height and width using CSS, it scales the entire canvas and compromises ...

Is your background image not filling the entire page?

Just diving into the world of HTML and CSS (with a touch of Bootstrap), I've set out to create a basic scrolling webpage. However, I've hit a roadblock with the background image not covering the full height of the page. It seems to stop at the h2 ...

Leveraging JavaScript along with the jQuery library and API to showcase information related to

Hey there! I have been working on this API that shows upcoming concerts, but I'm struggling to display the images associated with each concert. I've tried using for loops to iterate through the objects, and it seems like every sixth element has ...

div positioned on top of additional div elements

My HTML code consists of simple div tags <div class="left">Project Name: </div> <div class="right">should have a name</div> <div>Shouldn't this be on a new line?</div> These classes are defined in a stylesheet as ...

Is it still considered unsafe to send a password field using the GET method and AJAX?

Is it safe to send a password field with the get method using ajax? When utilizing an html form, the password can be visible in the URL. <form method='get'> <input type"password" name="pass" /> <input type="submit" valu ...

Introducing a fresh input field that includes a dropdown selection feature

When a user clicks on the "+" button, a new row should be added with a dropdown and input field. I want the input field name to be the dropdown value for each row. If "Facebook" is selected in the first dropdown, it should not appear in the second dropdo ...

Develop a distinctive JavaScript solution that enables an image to appear in fullscreen upon loading the page, and then smoothly fade away after precisely

I have been trying to incorporate a feature on my Shopify frontage website where our logo appears fullscreen and then fades away or disappears after a few seconds, allowing people to access the rest of the website. However, I am facing difficulty in making ...

Could someone assist me in understanding why VScode is displaying an error stating it cannot locate a module?

node:internal/modules/cjs/loader:1051 throw err; ^ Error: The module '/Users/ben/Desktop/GA/unit2/week5/GA_Project_2/QuotaQuest/index.js' cannot be found. at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15) at Modul ...

What is the best way to create a clickable color feature that triggers a function to change the fill of an SVG object when clicked by the user?

I am attempting to create a user-friendly color selection system where users can click on colors that will then be applied to fill an svg object, similar to a digital coloring book for children. I am struggling to figure out how to instruct JavaScript to s ...

PHP pattern matching equivalent to selecting elements based on their position in CSS

Struggling with the logic to identify objects in an array by their position. Seeking a PHP equivalent of nth-child(5n + 1) condition, which targets positions like 0, 5, 10, and so on within a loop. One approach could be to divide by 5 and check for whole ...

multi-event countdown timer

I have developed a simple system where a builder is assigned a task and upon completion, the inventory is updated accordingly. However, I am facing an issue with assigning multiple tasks. If task B is completed before task A, the inventory is updated with ...

Utilizing adjacent sibling selectors and the :before pseudo element in the latest Chrome 10 update

This is a unique situation. On the website , the list within the `` tags are annotated using the `:before` pseudo-element for chart labels (ABC and image). I have used adjacent sibling selectors in my CSS code to achieve this effect: #profiletext ol li:be ...

Issues with hover functionality on top navigation bar (CSS)

I encountered an issue with my HTML file. Being new to HTML, while creating a topbar for my static website here, I attempted to add a hover effect to it. However, the hover effect is only working correctly on the "search" and "cart" buttons (and partially ...

Transferring Django ORM data to Javascript for assessment

I have a situation where an ORM call retrieves data from a database. This data is then passed to an HTML template, but I also need it for a JavaScript function call. The process works as follows: The form loads and triggers the ORM call to fetch values f ...

Interactive form fields and showcasing data

Hi there, I'm not very familiar with jQuery but I need some help with the following code: jQuery(function($) { $('#precoInserido').bind('keydown keyup keypress', function() { $('#precoVer').html(this.value || "??") ...