JavaScript Inline Failures

Having trouble with this script? It seems to be working on fiddle but not locally. Can someone test it out and see if it works for them?

<HTML>

<HEAD><TITLE>Server Disk Alert</TITLE>
<script type="text/javascript" src="../../../Backup/2012/Web Projects/jquery-1.8.2.min.js"></script>
<script type="text/javascript">

document.ready(function(){
var widthval = parseInt($('.item').eq(0).text(), 10);
var $hr = $('hr').eq(0);
alert(widthval);
$hr.width(widthval);

if (widthval > 90) {
    $hr.css('background-color', 'red');}

});

</script>

</HEAD>

<BODY BGCOLOR="#EFEFFF">


<TABLE id="table_id" BORDER="1" CELLPADDING="2" CELLSPACING="2">
<TR>
 <TH COLSPAN="5" ALIGN="CENTER">Critical Server Disk Space</TH>
</TR>
<TR>
 <TH>Server</TH>
 <TH>Drive</TH>
 <TH>Percent Free Space</TH>
 <TH>Size Free Space</TH>
 <TH>Status</TH>
</TR>


<TR>
 <TD>%server%</TD>
 <TD>%drive%</TD>
 <TD class="item">50%</TD>
 <TD>%fspace%</TD>
 <TD width=200 style="border: 2px solid silver;padding:none">
 <hr style="color:#c00;background-color:blue;height:15px; border:none; margin:0;" align="left"/> </TD>
</TR>
<TR>

</TABLE>

</BODY>
</HTML>

Check out the jsfiddle link to see what I'm trying to achieve.
http://jsfiddle.net/XhxkX/7/

Answer №1

The reason your code is not functioning properly is due to the combination of regular JavaScript DOM methods and jQuery.

Try using the following revised code:

var width = parseInt($('.item').eq(0).text(), 10);
var $hr = $('hr').eq(0);

$hr.width(width);

if (width > 90) {
    $hr.css('background-color', 'red');
}​

Check out the live demonstration here: http://jsfiddle.net/XhxkX/11/

Furthermore, I noticed a couple of invalid HTML tags (<LPFOOTER> and <LPHEADER>) within your code. It seems like these tags are misplaced and one of them is missing an opening tag. It would be beneficial to update your HTML according to current standards before proceeding with any further modifications.

Answer №2

$(document).querySelectorAll("hr")[0].style.width = widthValue;

It should actually be:

document.querySelectorAll("hr")[0].style.width = widthValue;

Answer №3

Give this a shot:

<HEAD><TITLE>Server Disk Alert</TITLE>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script>
<script type="text/javascript" src="../../../Backup/2012/Web Projects/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var widthValue = document.getElementsByClassName("item")[0].innerText;
document.getElementsByTagName("hr")[0].style.width = widthValue;

if (parseInt(widthValue, 10) > 90) {
    document.getElementsByTagName("hr")[0].style.backgroundColor = "red";
}

});
</script>
</HEAD>

Answer №4

One issue you might be facing is the combination of javascript and jquery

Try implementing this solution instead:

 $(document).ready(function(){
var widthValue = $(".item").text();
$("hr").css("width",widthValue)
if (parseInt(widthValue, 10) > 90) {
   $("hr").css("backgroundColor","red");
}
});

Answer №5

In order to demonstrate how to access and modify DOM elements, I utilized two distinct methods. Hopefully, this will provide some clarity on the subject.

   <script type="text/javascript">

$(document).ready(function(){
var widthValue = parseInt($('.item').eq(0).text());
$('hr').eq(0).css('width',widthValue) ;

if (parseInt((widthValue.replace('%','')/10)* 10) > 90) {
   $('hr').eq(0).css({'background-color':'red'});
}

});

​​

Answer №6

Finally cracked it! Huge shoutout to an old pal who guided me through the process.. here's the secret code:

<!DOCTYPE html>
<html>
    <head>
        <title>Critical Server Disk Space</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){

                $('.bar').each(function(i){

                    var width = parseInt($('.item').eq(i).text(), 10);
                    var hr = $(this).eq(i);

                    $(this).width(width);

                    if(width >= 90){
                        $(this).css('background-color', 'red');
                    }
                });

            });

            //window.onload = init();
        </script>
        <style type="text/css>>
            hr{
                border:none;
                background:blue;
                height:15px;
                margin:0px;     
                text-align:left;
            }
            .border{border:1px solid #CCC; padding:0px; width:100px;background:#FFF;}
        </style>
    </head>
    <BODY BGCOLOR="#EFEFFF" >
        <TABLE BORDER="1" CELLPADDING="2" CELLSPACING="2">
            <TR>
             <TH COLSPAN="5" ALIGN="CENTER">Critical Server Disk Space</TH>
            </TR>
            <TR>
             <TH>Server</TH>
             <TH>Drive</TH>
             <TH>Percent Free Space</TH>
             <TH>Size Free Space</TH>
             <TH>Status</TH>
            </TR>
            <TR>
             <TD>%server%</TD>
             <TD>%drive%</TD>
             <TD class="item">50 <!-- 2% --></TD>
             <TD>%fspace%</TD>
             <TD width=200">
              <div class="border"><hr class="bar" /></div>
             </TD>
            </TR>
            <TR>
             <TD>%server%</TD>
             <TD>%drive%</TD>
             <TD class="item">30 <!-- 2% --></TD>
             <TD>%fspace%</TD>
             <TD width=200">
              <div class="border"><hr class="bar" /></div>
             </TD>
            </TR>
            <TR>
             <TD>%server%</TD>
             <TD>%drive%</TD>
             <TD class="item">95 <!-- 2% --></TD>
             <TD>%fspace%</TD>
             <TD width=200">
              <div class="border"><hr class="bar" /></div>
             </TD>
            </TR>
            <TR>
             <TD>%server%</TD>
             <TD>%drive%</TD>
             <TD class="item">80 <!-- 2% --></TD>
             <TD>%fspace%</TD>
             <TD width=200">
              <div class="border"><hr class="bar" /></div>
             </TD>
            </TR>
        </TABLE>
    </BODY>
</HTML>
</html>

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

Restricting the character count

I am trying to add a variable entered in an input field to a list item. I need to shorten the P <p class="layer' + count + '"> '+ Xia2 +'</p> text to only 15 characters without cutting off the SPAN, which also uses the same ...

A error was encountered stating that the formValidation function is not defined when the HTML form element is submitted

Having trouble calling a function and receiving an error message. How can I resolve this issue? The error message reads: index.html?email=&gebruikersnaam=&wachtwoord=&submit=Submit:1 Uncaught ReferenceError: formValidation is not defined at HT ...

Conceal the main div when the nested div does not contain any content

Is there a way to hide the parent div EventsRollup when the child div RelatedEventsList is empty? <div class="EventsRollup"> <span class="EventsRollupTitle">CPR &amp; Health Safety Classes</span><br /><br/> ...

Step-by-step guide to implementing a sticky header while scrolling

How can I implement a fixed header on scroll, like the one seen on this website: www.avauntmagazine.com Here is the HTML for my header: <div class="bloc bgc-wild-blue-yonder l-bloc " id="bloc-1"> <div class="container bloc-sm"> &l ...

Clicking on a React component will result in highlighting

What's the best way to display the selected item after a user clicks on it? <Box py={2}> <Grid container width="330px"> <Grid container direction="column" item xs align="left"> ...

How can I correctly link and open a PDF file in HTML without an extension provided in the filename?

Looking for some advice. I have a client project where there are PDF files uploaded to a file structure within a LAMP Stack, but the files do not have extensions. I need the browsers to recognize that these files are PDFs and open them accordingly. Adding ...

Personalized CSS for Modifying Highlight Color of Main Menu when Selected on WEN Associate Theme

I am currently working on customizing the appearance of my website's main menu. Specifically, I want to modify the small highlighted bar that appears under the selected item to better align with our brand colors. The default color in the WEN Associate ...

Is there a way to prevent my navbar from breaking buttons on mobile devices?

I am a beginner working on my practice portfolio and I have decided to make it fully responsive using flexbox as much as possible. I am following a "mobile-first" approach, so I can address any design issues for desktop later on. One problem I am facing is ...

What is the method for utilizing script bundles in pure HTML (not cshtml)?

After reviewing the following two questions on Stack Overflow, I made sure not to ask a duplicate question: 1) @Render.Scripts in plain .html file - not in .cshtml 2) Rendering the script bundle in a plain HTML page I am curious about how to incorporate ...

Creating Functions with HTML, CSS, and Javascript

I have been working on a website that is designed to convert numbers for you. Most of the code is complete, but I have encountered an error that I am currently unable to resolve. When I try to run the code, I see content on the page but when I click the ...

Sending JSON data from SpringMVC to Java server

I am working with a javascript array that is structured like this: var citizens1 = [{"startLat":null,"startLng":null,"socialSecurityNumber":null}]; The array contains 500 records, and instead of sending each record individually to the server, I convert ...

Distribute uniform width among child elements using CSS styling

In a fixed-width area, I may need to place either four divs or two divs based on different circumstances. If there are only two divs, they should occupy 50% of the width each: On the other hand, if there are four divs, they should be stacked like this: ...

The navigation bar isn't properly aligned with the image contained within

Currently, I am facing an issue with my layout where a container div is set to a width of 910px and contains an image of the same size. Below the image, there is a navigation bar made up of an Unordered list with links that change color on hover. For refer ...

Move the background image by scrolling it

Is there a way to scroll a background image until the end pixel and then go backwards to the beginning of the image, repeating this sequence continuously? Currently, I have a code that scrolls the background image continuously in one direction. How can I ...

Integrating an Interactive Map with PHP

Hello, I'm Chris and this is my initial request for assistance. I am currently encountering some challenges when it comes to embedding a Minecraft Server Dynamic Map into my PHP code. The map's URL is 209.105.236.244:8123 Here is the code snip ...

Positioning child divs using CSS

I am trying to arrange the #inner2 and #inner3 divs to float next to each other while also adjusting to fit perfectly within the #outer div when the browser window size changes. Unfortunately, inner3 is not floating correctly as it should be and is overlap ...

Blogger Extension - Cross Domain Communication

As I work on creating a blogger plugin, my goal is to send information from the blog page for analytic purposes and then display the results on the visitor's page. Initially, I tried sending the page content using an ajax call but encountered an error ...

What is preventing images from displaying in my slider within baguetteBox?

Currently, I am in the process of incorporating a slider into my website using the baguetteBox library. After consulting the documentation, I successfully implemented an example that is functioning smoothly without any issues. In order to display a series ...

Ways to retrieve the content and characteristics of a span element

I have this code snippet : <br> var value = $("#editor").jqxEditor('val'); value="<div><span style="font-weight:bold">Welcome</span></div>"; Now I want to extract the text inside the span tag and its font-weight a ...

Ways to extract information from a website using the data within a span element

I am struggling to extract the R1200 value from the "one Way drop off surcharge" section. Despite trying different methods, I have not been successful in retrieving this information. My goal is to copy the 1200 value and paste it into an Excel cell. I am f ...