How come my h1 heading gets obstructed by the input box when the slideUp function is triggered

On my wikisearch page, I have a title and input box positioned around the middle of the page. When I click on the button, the title slides up along with the input box. However, the input box ends up covering the title completely. I'm trying to figure out how to prevent this from happening or how to make sure the title stays at the top of the page. Any suggestions would be appreciated!

$(document).ready(function() {
  //bringing focus to search box
  window.load = function() {
    document.getElementById("search-box").focus();
  };

  //listener for search button
  $("#search").click(function() {
    $("#title").slideUp(3000);
    // $("#title").css("text-align", "left");
    search();
  });

  function search() {
    //grabbing the id of search result div
    var srchResult = document.getElementById("results");
    //string entered by user for search
    var searchStr = document.getElementById("search-box").value;
    //replace space with _ in search query
    searchStr = searchStr.replace(" ", "_");
    console.log(searchStr);

    $.ajax({
      url: "https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=" + searchStr + "&prop=info&inprop=url&utf8=&format=json",
      dataType: "jsonp",
      success: function(response) {
        if (response.query.searchinfo.totalhits === 0) {
          showError(searchStr);
        } else {
          displayResults(response);
        }
      },
      error: function() {
        alert("Something went wrong.. <br>" +
          "Try again!");
      }

    });

    function displayResults(response) {

      console.log(response.query);

      var search = response.query.search;
      var srchLength = response.query.search.length;

      srchResult.innerHTML = "";
      // console.log(srchResult.innerHTML);

      //pulling title and searchbox to top
      // $("#title").css("margin-top:", "10px !important");

      for (var i = 0; i < srchLength; i++) {
        srchResult.innerHTML += '<div class="output"><h4><a href="https://en.wikipedia.org/wiki/' + search[i].title.replace(' ', '_') + '" target="_blank">' + search[i].title + '</a> </h4><p>' + search[i].snippet + '</p></div>';

      }
    }
    return false;
  }

  function showError(search) {
    srchResult.innerHTML = '<div class="output text-center"><h4>No Search result for: ' + search + '</h4></div>';
  }
});
body {
  background-color: #495444;
}

search-input {
  width: 90%;
}

center {
  align-left: auto;
  align-right: auto;
  text-align: center;
}

.output {
  background-color: white;
  border-color: black;
  border-width: 1px;
  border-style: solid;
  opacity: 0.5;
  margin-top: 10px;
}

h1 {
  margin-top: 200px;
  color: #1484e5;
  font-family: 'Josefin Sans', sans-serif;
  font-size: 50px;
  padding-bottom: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Josefin+Sans" rel="stylesheet">
<div class="container ">
  <h1 id="title" class="text-center"><strong>WikiSearch</strong></h1>

  <div id="input" class="input-group col-lg-8 offset-lg-2 col-md-8 offset-md-2 col-xs-12">
    <input id="search-box" type="text" class="form-control" placeholder="Search Wikipidia Pages!..." />
    <button id="search" class="btn btn-primary" onclick="#">Search</button>
  </div>

  <div id="results" class="col-lg-8 offset-lg-2">

  </div>
</div>

Answer №1

Instead of utilizing $('#title').slideUp(3000), consider using

$('#title').animate({'margin-top': '0'}, 3000);

By doing so, the title will remain in place.

Additionally, it's advisable to remove onclick="#" from

<button id="search" class="btn btn-primary" onclick="#">Search</button>

An example is provided below.

$(document).ready(function() {
  //bringing focus to search box
  window.load = function() {
    document.getElementById("search-box").focus();
  };

  //listener for search button
  $("#search").click(function() {
  $('#title').animate({'margin-top': '0'}, 3000);
    //$("#title").slideUp(3000);
    // $("#title").css("text-align", "left");
    search();
  });

  function search() {
    //grabbing the id of search result div
    var srchResult = document.getElementById("results");
    //string entered by user for search
    var searchStr = document.getElementById("search-box").value;
    //replace space with _ in search query
    searchStr = searchStr.replace(" ", "_");
    $.ajax({
      url: "https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=" + searchStr + "&prop=info&inprop=url&utf8=&format=json",
      dataType: "jsonp",
      success: function(response) {
        if (response.query.searchinfo.totalhits === 0) {
          showError(searchStr);
        } else {
          displayResults(response);
        }
      },
      error: function() {
        alert("Something went wrong.. <br>" +
          "Try again!");
      }

    });

    function displayResults(response) {


      var search = response.query.search;
      var srchLength = response.query.search.length;

      srchResult.innerHTML = "";
      // console.log(srchResult.innerHTML);

      //pulling title and searchbox to top
      // $("#title").css("margin-top:", "10px !important");

      for (var i = 0; i < srchLength; i++) {
        srchResult.innerHTML += '<div class="output"><h4><a href="https://en.wikipedia.org/wiki/' + search[i].title.replace(' ', '_') + '" target="_blank">' + search[i].title + '</a> </h4><p>' + search[i].snippet + '</p></div>';

      }
    }
    return false;
  }

  function showError(search) {
    srchResult.innerHTML = '<div class="output text-center"><h4>No Search result for: ' + search + '</h4></div>';
  }
});
body {
  background-color: #495444;
}

search-input {
  width: 90%;
}

center {
  align-left: auto;
  align-right: auto;
  text-align: center;
}

.output {
  background-color: white;
  border-color: black;
  border-width: 1px;
  border-style: solid;
  opacity: 0.5;
  margin-top: 10px;
}

h1 {
  margin-top: 200px;
  color: #1484e5;
  font-family: 'Josefin Sans', sans-serif;
  font-size: 50px;
  padding-bottom: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Josefin+Sans" rel="stylesheet">
<div class="container ">
  <h1 id="title" class="text-center"><strong>WikiSearch</strong></h1>

  <div id="input" class="input-group col-lg-8 offset-lg-2 col-md-8 offset-md-2

 col-xs-12">
    <input id="search-box" type="text" class="form-control" placeholder=

 "Search Wikipidia Pages!..." />
    <button id="search" class="btn btn-primary">Search</button>
  </div>

  <div id="results" class="col-lg-8 offset-lg-2">

  </div>
</div>

Answer №2

Include this in the h1 style

h1 {
  z-index: 1000;
}

If you want a specific element to appear above the header, assign its class a z-index higher than 1000, like 1001! For elements that should be positioned behind, use a value of 999 or lower. Setting the z-index to 1000 allows for flexibility in arranging elements both in front and behind.

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

Error in setting jQuery cookie causing malfunction

Jquery: // handle loading content $(".link").click(function(e) { e.preventDefault(); $(".nav_head li a").removeClass('active-link'); $(this).addClass('active-link'); var title = $(this).text(); $(".head .blue").text ...

What could be causing the side margins on my navbar in mobile view in Bootstrap?

After spending some time working on a simple navbar with CSS styling, I encountered an issue when viewing it in mobile mode. The navbar did not expand to 100% of the screen width and had a margin of about 20px on both sides. CSS .navbar .brand { wi ...

Eliminate empty space in the table cell

I am working with a table that looks like this: <table> ... <td> <div class="btn-group btn-group-sm"> <button class="btn btn-info mini"><span class="glyphicon glyphicon-duplicate"></span></button&g ...

Issue with BeautifulSoup(page.content,'html.parser') not returning accurate content during web scraping

While attempting to scrape data from the AJIO website, I encountered an issue where the content retrieved by Python did not match what I saw when inspecting the exact webpage. It appears that there is some JavaScript code present on the page which dynamica ...

The issue with running commands in parallel using npm remains unresolved

Within my project folder, I have a package.json file located at the root directory. This JSON file contains separate client and server folders. In the main package.json file, I have defined the following scripts: "scripts": { "server&quo ...

Assistance needed with selecting a CSS rule

I have a question regarding CSS that seems pretty straightforward. Here is the code in question: <!doctype html> <html amp lang="en-US"> <head> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Slab: ...

Select a variable by referencing a specific div ID

As I work with jQuery, I am currently focusing on obtaining the ID of an element when it is clicked on the page. My goal is to use this ID to determine which variable should be utilized in my JavaScript code. For instance: When I click on the element wit ...

A step-by-step guide on selecting a checkbox within an alert popup using Selenium with Java

Hello everyone, I am struggling to find a solution for checking and unchecking a checkbox located in an alert window or modal pop-ups. We have three types of pop-ups: alert, confirm, and prompt. Specifically, in the confirm popup, there is a checkbox t ...

Updating image properties with JQuery

My challenge involves dealing with an image that includes the following code: <img src="/Modules/Visualiser.php?template=1&text=testing"> The goal is to change the 'template' parameter based on a selection made in a dropdown menu. How ...

Performing conditional aggregation in MongoDB through a collection of data

I attempted to update a MongoDB record conditionally using the code snippet below within a transaction. db.collection(SESSIONS_COLLECTION) .updateOne({_id: ObjectId(id)}, { $set: { end: { $cond: { if: { $gt: ["$en ...

Ways to incorporate a focus event into a template

I'm currently working on implementing autocomplete functionality in the search box on my homepage. To achieve this, I have included an onfocus event for the template to execute a jQuery command targeting the source elements. The method I used to add j ...

How to create a unique splash screen for mobile devices when the site is opened

What is the typical method for implementing a splash screen that appears when accessing a website from a mobile device? This screen usually contains links to native apps and an option to proceed to the full site. I am considering utilizing to detect the ...

When padding is added on an MVC 5 page, the Bootstrap 4.1.1 class option for rounded-circle becomes distorted

Incorporating VS 2017 and bootstrap 4.1.1 into an MVC 5 page, I am facing a challenge in adding right-side padding to an image without distorting the circular shape of the image, as shown below. When I apply the padding, the circle becomes warped, but remo ...

The HTML document declaration is missing an error

I encountered an error in my code, here is the snippet: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1. ...

Simple method for validating a text box field using jQuery without the need for a form

I am looking for a way to activate jquery unobtrusive validation without a form using the specified pattern: <script type="text/javascript"> $('.btnAddAsync').click(function (e) { e.preventDefault(); // Avoid href p ...

Utilizing JavaScript Objects within DWR Method Invocation

Having trouble passing a JavaScript Object to the server side using a DWR method call and encountering a JS error. Here is the JavaScript code: var referenceFieldValues = new Object(); var refFieldArray = referenceFields.split(","); for(var i=0;i<refF ...

Please upload the image by clicking the "Upload Files!" button instead of relying on the input change

I am looking to enhance my image uploading process by allowing users to upload multiple images after clicking the Upload Files! button, rather than relying on the input change event. Below is the jQuery code I am using (which I found here): index.html &l ...

Using CSS transforms to place the vanishing point

I am attempting to utilize CSS transforms to create a flat 'floor' that extends infinitely towards the horizon. The vanishing point, where the floor disappears into the distance, should align halfway up the browser window. I have encountered an ...

Error: Next.js is throwing a SyntaxError due to encountering an unexpected token 'export'

I encountered an issue when trying to render the following code: SyntaxError: Unexpected token 'export' (project path)/node_modules/react-syntax-highlighter/dist/esm/styles/prism/index.js Everything seems to work as expected initially, but then ...

What is the best way to implement a JavaScript pattern matching for both "aaaa" and "aaa aaa"?

Can anyone help me create a pattern that can accept both strings with spaces and without spaces in the same text box? I would appreciate any guidance on how to achieve this. Thanks! ...