Tips for repairing a button using a JavaScript function in an HTML document

I am currently working on extracting titles from body text. To achieve this, I have created a button and linked my function to it. The issue I am facing is that when I click on the button, it disappears from its original position. I intend to keep it in place as I plan to add more buttons with different functions. Is there a way to ensure that the button remains visible at all times?

Initially, I tried using the fixed position property, but I soon realized that it only fixes the button to the top of the screen when scrolling down, which is not what I want. Additionally, I noticed that the text formatting changes when I extract the titles. Can anyone explain why this happens?

Your assistance in resolving these issues would be greatly appreciated. Below is the current code snippet:

<html>
    <head>
        <meta charset="utf-8">
        <title>test</title>
        <style>
            #navigation li{
                display:inline;
                postion:absolute;
            }
            #navigation a{
                padding:2px 2px;
                background-color:#09F;
                color:#FFFFFF;
            }
            #navigation a:hover{
                background-color:#F90;
                color:#666;
            }
        </style>
        <div id="navigation">
            <a href="#"><input type=button onclick="myFunction()">
        </div>
    </head>
    <body>
        <div>
            <p id="demo">
                <pre> 
                    <b>This is one title</b>

                    I'm writing here
                    the text that I 
                    don't need to get.

                    <b>Other title</b>

                    And so we'll test
                    whether this thing works.
                </p>
            </pre>
        </div>
        <script>
            function myFunction() {
                var text = document.body.innerText;
                var titles =text.match(/^\n(.+?)\n\n/mg);
                for (var i = 0; i < titles.length; i++) {
                    document.write(titles[i] + "<br />" + "<br />");
                }
            }
        </script>
    </body>
</html>

Answer №1

Essentially, there seems to be an issue with having an opening <a> tag without a corresponding closing one. This mistakenly turns your entire website into a link. Additionally, your HTML contained various errors such as mismatched closing tags and HTML code placed between </head> and <body>.

To correct these issues, your HTML should be structured like the following:

<html>
<head>
    <meta charset="utf-8"/>
    <title>test</title>
    <style>
        #navigation li{
            display:inline;
            postion:absolute;
        }
        #navigation a{
            padding:2px 2px;
            background-color:#09F;
            color:#FFFFFF;
        }
        #navigation a:hover{
            background-color:#F90;
            color:#666;
        }
    </style>
</head>
<body>
    <div id="navigation">
        <input type=button onclick="myFunction()" />
    </div> 
    <div>
        <p id="demo">
            <pre> 
                <b>This is one title</b>

                I'm writing here
                the text that I 
                don't need to get.

                <b>Other title</b>

                And so we'll test
                whether this thing works.
            </pre>
        </p>
    </div>

    <script language="javascript">
        function myFunction() {
            var text = document.body.innerText;
            var titles =text.match(/^\n(.+?)\n\n/mg);
            for (var i = 0; i < titles.length; i++) {
                document.write(titles[i] + "<br />" + "<br />");
            }
        }
    </script>
</body>
</html>

Answer №2

Check it out here! I've made some updates to the HTML, improved its semantic structure by removing the onclick attribute, and simplified the algorithm using document.getElementsByTagName().
Take a look at the revised code:

var r = document.getElementById('results');

document.getElementById('btn').onclick = myFunction;

function myFunction() {
    var titles = document.getElementsByTagName('b');
    for (var i = 0; i < titles.length; i++) {
      r.innerHTML += titles[i].innerHTML + "<br />" + "<br />";
    }
}

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

ZK: maintaining session connectivity

When I need to redirect to a tel:**** link in ZK and then redirect the user to another page after the call, I encounter a problem. Every time I click on the link, ZK interprets it as leaving the browser, which results in my client session ending automatica ...

Is there a way to provide "only responsive" content to the mobile m. subdomain without the need for redirection?

I currently have a website with www. that redirects to different content on the mobile m. site. I am in the process of updating my site to be responsive, but I want to ensure that I do not lose the links to the m. site in Google SERP. How can I redirect m ...

iOS now supports PowerPoint presentations through the use of the .pptx file format

Attempting to incorporate a pptx presentation with videos and animations into an iOS native app has proved challenging. Although UIWebView successfully renders the presentation allowing for slide browsing, it lacks interactivity such as button clicks that ...

html pagination on website

I am currently displaying my products in rows, however the code I'm using is presenting them in columns instead. I have implemented a datatable pagination feature, but I'm struggling to understand how to properly use it on my website. Can someone ...

Guidance sought on utilizing a callback to retrieve data from mysql using node.js and ejs

I am trying to retrieve the value of foo from the query provided below: exports.get = function(id, cb) { sql = 'SELECT `sidebar`, `test` FROM users WHERE `id` = "' + id + '"'; con.query(sql, function(err, foo) { if (err) ...

Is the sequence of CSS styles important in styling web content?

I'm a newcomer to this field and recently encountered an issue with my div styling. My code was compatible with Chrome 11 but did not render properly in Firefox 4, Opera 11.1, and IE 8. Upon review, I found that the style for the div was as follows, a ...

Create original identifiers for dynamically generated material

I have developed a custom invoice tool that has the capability to dynamically add rows. Here is the code snippet responsible for generating new rows: $("#addrow").click(function(){ $(".item-row:last").after('<tr class="item-row"><td> ...

Is it necessary for Webpack to process the index.html file for Vue applications?

I am facing an issue with my index.html file where the assets inside the head tag are not loading as images but as the index.html file itself. I have included the following links in my head tag: <link rel="apple-touch-icon" sizes="60x60" href="./assets ...

Trigger javascript function with a button click

Is there a way to trigger a JavaScript function from an HTML button that is not functioning properly? REVISED To see the issue in action, please visit this jsfiddle link: http://jsfiddle.net/winresh24/Sq7hg/341/ <button onclick="myFunction()">Try i ...

Experimenting with NGXS selectors: A comprehensive guide

Hey there, I am currently utilizing the NGXS state management library in my application. I have a selector set up like this and everything seems to be functioning correctly. However, while testing the app, I encountered the following error message: "PrintI ...

How to Pass Values in handleChange Event in Typescript

I have noticed that most online examples of handling change events only pass in the event parameter, making the value accessible automatically. However, I encountered an error stating that the value is not found when trying to use it in my handleChange fun ...

Is there a way to execute a JavaScript function by clicking on an anchor tag?

Many websites, such as StackOverflow, have attempted to address this particular issue. However, the explanations provided are complex and difficult for me to grasp. I am looking for someone who can help identify my mistakes and explain the solution in simp ...

Struggling to create a drop-down box with CSS and not achieving the expected results

Currently, I am delving into the world of HTML and CSS to hone my skills in front-end web development. While attempting to code a drop-down box within my navigation menu, I have encountered an issue where the drop-down opens at the left corner while the na ...

Is Eval really as bad as they say... What alternative should I consider using instead?

After making an ajax request, a JSON array filled with user inputs is returned to me. The inputs have already been sanitized, and by utilizing the eval() function, I can easily generate my JavaScript object and update the page... However, there lies a dil ...

Can you conceal the label while also displaying the placeholder?

Within my form, I am using the following code: <div id="zipcode-label" class="f-label"><label for="zipcode" class="required">Zip code</label></div> <div id="first-element" class="f-element"><input type="tel" class='ro ...

What is the best way to create a sequential number pattern of 1, 2, 3, 4 in Mongoose?

Is there a way to create a similar pattern without coding experience? I'm not familiar with coding, so looking for guidance. Here is the code snippet in question: array = 1,2,3,4,5 const note = new notedModel ({ _id: array, note: args[1] ...

The display of the selected input is not appearing when the map function is utilized

I am attempting to use Material UI Select, but it is not functioning as expected. When I use the map function, the default value is not displayed as I would like it to be. However, it does work properly when using the traditional method. *** The Method th ...

The images in Bootstrap-Grid at 1920 resolution stay compact

Is there a way to make images grow beyond the 1280 resolution? This is how it appears at 1920 resolution https://i.sstatic.net/JmYc7.png And this is at 1280 resolution https://i.sstatic.net/Sk19K.png <div id="logokutu1" class="d ...

Nextjs: where all roads lead back to the homepage

Having an issue in my app where all redirects keep leading back to the homepage. The problem seems to stem from my Navbar.js component, as shown below. Despite adding the required route in the Link tag for next-js, both client and server compilation is suc ...

Creating a scrolling background effect using HTML and CSS: ``Background Parallax

I'm trying to keep the background of my HTML document fixed in the upper left corner, so that when a user scrolls through the page, they only see parts of it. Here's the code I'm using: <body style="background-image: url(background.png) ...