What is the best way to connect two buttons in separate divs?

I'm facing a challenge of adding two buttons side by side when they are located in different div elements. I've tried using the float property in the btn-group, but it interferes with the functionality of the dropdown and affects the animation. How can I resolve this issue and have the 'Add' and 'View' buttons positioned next to each other?

Live Example

window.onload = function() {
    document.getElementById('hucontainer').style.display = 'none';
};
$('#divNewNotificationsRole-admin li').on('click', function() {
    $('#role-admin').html($(this).find('a').html());
});

// Code for handling and toggling dropdowns

// Event listener for selecting the first dropdown element
$('#role').click(function(e){
  e.preventDefault(); // Prevent default action
  var hu = document.getElementById("hucontainer");
  var dropdowns = document.getElementsByClassName("dropdown-menu");
  hu.style.display = 'none';
  $("#rolecontainer").show("slow", function(){

  });
  dropdowns.style.display='none';
});

// Event listener for selecting the second dropdown element
$('#relation').click(function(e){
  e.preventDefault();
  var role = document.getElementById("rolecontainer");
  var dropdowns = document.getElementsByClassName("dropdown-menu");
  $("#hucontainer").show("slow", function(){

  });
  role.style.display = 'none';
  dropdowns.style.display = 'none';
});
...

Answer №1

To modify the button's position, you can apply some styling:

#add{
  display: inline-block;
  position: absolute;
  top: 33px;
  left: 85px;
}

window.onload = function() {
  document.getElementById('hucontainer').style.display = 'none';
};
$('#divNewNotificationsRole-admin li').on('click', function() {
  $('#role-admin').html($(this).find('a').html());
});

// View dropdown

// If the first dropdown element is selected, hide the second element and the dropdown list
$('#role').click(function(e){
  e.preventDefault(); // Select dropdown element without reloading the page
  var hu=document.getElementById("hucontainer");
  var dropdowns = document.getElementsByClassName("dropdown-menu");
  hu.style.display='none';
  $("#rolecontainer").show("slow", function(){

  });
  dropdowns.style.display='none';
});

$('#relation').click(function(e){
  e.preventDefault();
  var role=document.getElementById("rolecontainer");
  var dropdowns = document.getElementsByClassName("dropdown-menu");
  $("#hucontainer").show("slow", function(){

  });
  role.style.display='none';
  dropdowns.style.display='none';
});
#add{
  display: inline-block;
  position: absolute;
  top: 33px;
  left: 85px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="container">
  <div align="right" class="dropdown">
      <button class="btn btn-primary dropdown-toggle btn-md" type="button" data-toggle="dropdown"><i class="fa fa-user"></i> Admin
          <span class="caret"></span>
      </button>
      <div class="dropdown-menu">
          <li><a class="dropdown-item" href="#logout" data-toggle="modal">Logout <i class="fa fa-sign-out"></i></a></li>
          <li><a class="dropdown-item" href="#changepass" data-toggle="modal">Change Pass <i class="fa fa-undo"></i></a></li>
      </div>
  </div>
  <div class="btn-group" id="divNewNotificationsRole-admin">
      <button id="role-admin" type="button" class="btn btn-success dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      View</button>
      <div id="myDropdown" class="dropdown-menu">
          <li><a id="role" class="dropdown-item" href="#">First Div</a></li>
          <li><a id="relation" class="dropdown-item" href="#">Second Div</a></li>
      </div>
  </div>
</div>
<br/>

<div id="rolecontainer" class="container">  
  <div class="table">  
      <div align="right">  
          <button type="button" name="add" id="add" data-toggle="modal" data-target="#add_data_Modal" class="btn btn-warning">Add <li class="fa fa-plus"></li></button>  
      </div>  

      <h4><b>First Div</b></h4><br/>
      <div id="role_table">  
          <table id="szerep_data" class="table table-striped table-bordered" cellspacing="0" width="100%">   
              <thead>
                  <tr>  
                      <td align="center" width="65%"><b>Data</b></td>  
                      <td align="center" width="35%"><b>View</b></td>  
                  </tr>  
              </thead>
              <tbody>
                  <tr>
                      <td>Data1</td>
                      <td><button class="btn btn-info">View</button></td>
                  </tr>
                  <tr>
                      <td>Data2</td>
                      <td><button class="btn btn-info">View</button></td>
                  </tr>
              </tbody>
          </table>  
      </div>  
  </div>  
</div>  

<!-- Second dropdown element -->
<div id="hucontainer" class="container">
  <div class="col-sm-7">
      <div align="right">  
          <button type="button" name="add" id="addHu_button" class="btn btn-warning">Add <i class="fa fa-plus"></i></button> 
      </div> 
      <h4><b>Second Div</b></h4>
      <br/>
      <div id="hu_table">
          <table id="order_data" class="table table-striped table-bordered" cellspacing="0" width="100%">
              <thead>
                  <tr>
                      <td align="center"><b>Data</b></td>
                      <td align="center"><b>View</b></td>
                  </tr>
              </thead>
              <tbody>
                  <tr>
                      <td>Data1</td>
                      <td><button class="btn btn-info">View</button></td>
                  </tr>
                  <tr>
                      <td>Data2</td>
                      <td><button class="btn btn-info">View</button></td>
                  </tr>
              </tbody>
          </table>
      </div>
  </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

Exporting an HTML table to Excel while excluding any hidden <td> elements

I have a web application with an HTML table that displays data to users. I wanted to export the table to Excel and found a jQuery code for it. However, the exported data includes information that is hidden in the table. Is there a way to export the tabl ...

What is the best way to move table data content to a new line?

I have a table that spans the entire width of the screen, with 3 <td> elements inside. The content in the 2nd <td> does not contain any spaces and is quite long, causing it to expand beyond its allotted 70% width and disrupt the layout. Is ther ...

Initiate the setInterval function upon clicking the button

Is there a way to successfully start setInterval when the user presses a button with ID #begin? I have attempted various methods, but they all seem to prevent setInterval from working at all. Any suggestions on how to make this work correctly? Thank you! ...

Do AJAX calls necessitate the use of 200 OK messages to successfully execute a server side request?

We are working on a tracking system that involves making three consecutive ajax calls within the WordPress environment using the WordPress AJAX API. On the third call, it triggers a page refresh. I have observed that sometimes one of my data entry attempt ...

How can I showcase Bootstrap cards horizontally within a razor foreach iteration?

I seem to be experiencing a mental block on how to arrange bootstrap cards in a horizontal layout using a for each loop. By default, they appear vertically. Below is my razor code: @foreach (var item in Model) { var imgUrl = Url.Content("~/Conte ...

Can you rely on a specific order when gathering reactions in a discord.js bot?

Imagine a scenario where a bot is collecting reactions to represent event registrations. To prevent any potential race conditions, I have secured the underlying data structure with a mutex. However, the issue of priority still remains unresolved as user # ...

An issue observed in MVC 5 with Json requests is the absence of the final dropdown option in a cascading dropdown menu

While choosing items from my cascading dropdown menus, I notice that the last item in the list is missing until I make a selection. Once an option is chosen, the final item appears at the end of the list and becomes visible and selectable. ViewModel pub ...

There is an array present with data filled in, but unfortunately, I am unable to retrieve specific elements

As I work on my WordPress 4.7.2 website, I find myself making an AJAX call. The PHP function handling this call returns an array using wp_json_encode(). When I view the data array in the success callback of the AJAX function, everything looks just as expec ...

When using Javascript, an error is being thrown when attempting to select a nested element, stating that it is not a function

I am facing a challenge in selecting an element within another element, specifically a button within a form. Typically, I would use jQuery to achieve this as shown below: element = $('#webform-client-form-1812 input[name="op"]'); However, due t ...

AJAX: Building a Robust Single Page Application with Enhanced Security

Currently, I am developing a web/mobile application using AJAX. This app consists of 4 pages: the login page and three protected pages that are only accessible to logged-in users. My plan is to implement the Single Page Application pattern, where all 4 pa ...

How can I use AngularJS to initiate code to run after the view and controller have successfully synchronized a specific change?

I am currently using AJAX to load date ranges into a pair of SELECTs (managed with AngularJS ng-repeat), which are then transformed into a slider using jQuery UI's selectToUISlider. My concern is that selectToUISlider may behave unexpectedly if the SE ...

How can we align divs in the center horizontally inside a parent div?

This question has been asked many times, but I can't seem to figure it out in my situation as I am new to HTML. I have three divs (boxes: QM, Audit, PM) and I want to center them within a parent div called Services. In the photo provided, you can see ...

Empty Array returned in VueJS after Axios GET request | VUEJS/LARAVEL

Currently, I am working on a project for my university while also learning how to build a single page application through a Udemy course. The issue I'm facing is related to pushing data from a JSON database query named "alunos" to the front end using ...

What is the best way to adjust the z-index of a dropdown auto complete to appear above a background image?

Is there a way to adjust the z-index of the drop-down autocomplete box so that it appears above a background image? Take a look at this screenshot: https://i.sstatic.net/H90jV.png The four images below are background images styled with sprite CSS. The a ...

Pros and cons of leveraging a hybrid HTML/Objective-C app for the iPhone platform

When creating an app, what are the key benefits or drawbacks to consider when deciding between utilizing HTML/UIWebView for certain parts (such as styled menus and pages with complex layouts) versus going completely native? I'm eager to learn from ot ...

Ways to verify the presence of an item in a MonoDB array

My MongoDB model looks like this: const userSchema = new Schema = ({ name: { type: String }, company: [ companyId: { type: String, }, movies: [ { genre: { type: String, enum: [ ...

Ensure that the navbar remains within the container as you scroll down the page

With the implementation of bootstrap 4, I have successfully integrated a fixed navbar inside a container. Initially, at the top of the page, it occupies the full width, but as soon as I start scrolling, it smoothly transitions into the confines of the cont ...

Differentiate among comparable values through placement regex

I'm currently tackling a challenge involving regex as I work on breaking down shorthand CSS code for the font property. Here is my progress thus far: var style = decl.val.match(/\s*(?:\s*(normal|italic|oblique)){1}/i); style = style ? style ...

I'm looking to include an icon in my TreeItem component in Material UI 4. How

I have a unique menu created using MATERIAL UI 4, consisting of a primary TreeItem for the main menu and a secondary TreeItem for the submenu sections. I am looking to assign a specific icon to each main menu label. How can I achieve this? Additionally, ...

Complete the form by following a hyperlink located on a different webpage

I recently developed three PHP pages. The first one consists of the following form: <form method="POST" name="go" action="search_form_all.php" > <input name="value" type="text" id="search_form_1" size="65" placeholder="enter name"/> <i ...