Using JavaScript to fetch elements by their ID along with a button

UPDATE: I have corrected the semi-colons, case sensitivity, and brackets in the code. It functions properly if I eliminate the functions after buttonPARTICULAR! Why is that?

UPDATE: Issue resolved. My mistake. Apologies!!! :-Z

When I simplify it like this, everything runs smoothly:

  <head>
  <script>

     function buttonGRUPO()
     {
         document.getElementById("desc").innerHTML="Grupo";
     }
     function buttonPARTICULAR()
     {
         document.getElementById("desc").innerHTML="Particular";
     }

 </script>
 </head>
 <body>

    <p>&nbsp;<button class="art-button" onclick="buttonPARTICULAR()">Aulas Particulares</button></p>
    <p>&nbsp;<button class="art-button" onclick="buttonGRUPO()">Aulas em Grupo</button></p>
  <p id="desc">Text</p>
 </body>

However, adding more functions to the script causes ALL buttons and code to cease functioning.

 <head>
 <script>

function buttonGRUPO()
{
    document.getElementById("desc").innerHTML="Grupo";
}
function buttonPARTICULAR()
{
    document.getElementById("desc").innerHTML="Particular";
}
function buttonINCOMPANY()
{
    document.getElementbyID("desc").innerHTML="in-COmpany";
}
function buttonINTENSIVOS()
}
    document.getElementbyID("desc").innerHTML="Intensivo";
{
function buttonIMERSIVOS()
}
    document.getElementbyID("desc").innerHTML="Imersivos"
{
function buttonPALESTRAS()
}
    document.getElementbyID("desc").innerHTML="Palestras"
}
</script>
</head> 
<body>
 <p>&nbsp;<button class="art-button" onclick="buttonPARTICULAR()">Aulas Particulares</button></p>
 <p>&nbsp;<button class="art-button" onclick="buttonGRUPO()">Aulas em Grupo</button>  </p>
 <p id="desc">Text</p>
 </body>

Why does increasing the amount of nearly identical code disrupt even the previously functional code? Is there a syntax error here?

UPDATE

@Woot4Moot Still not working

still not working.

function buttonB()
 {
document.getElementById("desc").innerHTML="B";
}
function buttonA()
{
document.getElementById("desc").innerHTML="A";
}
function buttonC()
{
document.getElementbyID("desc").innerHTML="C";
}
function buttonD()
{
document.getElementbyID("desc").innerHTML="D";
}
function buttonE()
{
document.getElementbyID("desc").innerHTML="E";
}
function buttonF()
{
 document.getElementbyID("desc").innerHTML="F";
}

<p><button onclick="buttonA()">A</button></p>
    <p><button onclick="buttonB()">B</button></p>
<p id="desc"> text</p>'

To clarify further, when I view this code in my browser (the buttons and p id="desc") but upon clicking the buttons, the content within p id="desc" does not change as expected based on the JavaScript command.

Answer №1

Aside from the syntax errors:

function buttonINTENSIVOS() {
    document.getElementbyID("desc").innerHTML="Intensivo";
}

function buttonIMERSIVOS() {
    document.getElementbyID("desc").innerHTML="Imersivos";
}

function buttonPALESTRAS() {
} 

Your curly braces are incorrectly placed. Functions should start with { not end with }, and you need to add semicolons ; at the end of each command.

The correct format is:

function buttonINTENSIVOS()
{
    document.getElementbyId("desc").innerHTML="Intensivo";
}

function buttonIMERSIVOS()
{
    document.getElementbyId("desc").innerHTML="Imersivos";
}

function buttonPALESTRAS()
{  ... }

Answer №2

In addition to the errors mentioned earlier (incorrect opening/closing braces), it's worth noting that JavaScript is case sensitive. Therefore, your method name is incorrect:

document.getElementbyID("...")

The correct syntax should be:

document.getElementById("....")

Please pay attention to the lowercase d in Id and the uppercase B in By.

Answer №3

To streamline and simplify your code for better readability and debugging, consider consolidating the individual functions using the following combined function:

function displayLetter(letter)
{
  document.getElementById("desc").innerHTML = letter;
}

Then in your HTML:

<p><button onclick="displayLetter('A')">A</button></p>
<p><button onclick="displayLetter('B')">B</button></p>
<p><button onclick="displayLetter('C')">C</button></p>

<p id="desc"></p>

Ensure to use single quotes within each set of parentheses. This function will directly display the content inside the parentheses to the p id=desc element.

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

Using knockout to retrieve the attribute value with an onClick event

Snippet of HTML View with attribute value 'Qref'. This is the sample HTML Code for binding Currently, I have manually inputted the Qref Attribute value <!--ko if:$parent.Type == 2 --> <input type="checkbox" data-bind="attr:{id: $data ...

Combine two elements in an array

I am faced with a challenge in binding values from an Array. My goal is to display two values in a row, then the next two values in the following row, and so on. Unfortunately, I have been unable to achieve this using *ngFor. Any assistance would be greatl ...

How do I incorporate scrolling into Material-UI Tabs?

I am currently incorporating Material-ui Tablist into my AppBar component. However, I am facing an issue with the responsiveness of the tabs. When there are too many tabs, some of them become hidden on smaller screens. For more information on the componen ...

Positioning Images at the Top of the Screen in React Native

I have two images that I would like to display side by side at the top of the screen. <View style={styles.container}> <View style={styles.topWrapper}> <Image source={TOP_START_GRAPHIC} style={styles.topStartImage} /> ...

Incorrect Tooltip DisplayWhat could be causing the issue with

I am facing an issue when trying to add a tooltip to a glyphicon within a tile. It doesn't seem to work correctly when it should. However, placing the tooltip outside of the tile works fine. I'm quite perplexed and would greatly appreciate any as ...

Toggle the visibility of a section in an external .js file

Is there a way to toggle the display of answers from an external .js file using a button? If I were able to modify the code, I could wrap the answers in a div. However, since it's an external .js file, is this feasible? Here's the fiddle and cod ...

What methods are available for updating the href color of an element using the DOM?

I am looking to update the color of a tab (Mathematics-tab) based on the value of 'aria-selected' changing to true in Bootstrap. I have multiple tabs, including one for mathematics, and I want to visually differentiate between selected and unsele ...

Utilize Ajax to automatically populate a textbox with suggestions

I'm retrieving data via an AJAX call. How can I bind the data for auto-completion in a text box using both the name and ID as fields? What is the best way to bind this data in the frontend and retrieve the selected name's ID in the backend using ...

Stopping the webdriver in Node.js gracefully without crashing it can be achieved through a

Can the selenium webdriver be halted without terminating node? I've encountered a problem attempting to create an API tool that conducts web automation upon receiving a get request. Essentially, I am executing the selenium webdriver on a get request ...

What is the method for adding a prefix to every line in JavaScript?

I'm currently working on a React project and dealing with a string that may contain newlines. I'm looking for the most efficient way to inject a symbol at the start of each line. I've considered exploding the string into an array and adding ...

Number entered isn't visible in Bootstrap modal window

Can anyone help me troubleshoot why the value "APno" is not appearing next to "Appointment Number" in a Bootstrap modal despite the modal showing up? This is my code: Form <form method="POST> Appointment Number:<br> <input type="n ...

Using JavaScript to extract the metadata of an image from a remote location

Is there a way to extract XMP metadata from a JPEG file using JavaScript? I came across a method for doing it in PHP (How can I read XMP data from a JPG with PHP?) which can be adapted for JavaScript using AJAX. However, the issue arises when trying to acc ...

Guide on incorporating an Angular directive to substitute an element under specific conditions

My goal is to create a directive that replaces an anchor element with a div element under certain conditions. The content of the anchor element should remain unchanged and be included within the new element. The concept involves having an attribute on the ...

The mouse scurries away once the div height has been adjusted

How can I make the height of #header change when hovering over #hoverme, and then revert back to its original height when the mouse leaves #hoverme? If anyone knows a solution, please check out my jsfiddle as it's not working as I intended. Here is ...

Modify the structure of the JSON string

My JSON string is structured like this: [ { "queryResult": { "A": "12-04-2014", "B": 1 } }, { "queryResult": { "A": "13-04-2014", "B": 2 } }, { "qu ...

Utilizing JSON data to generate an HTML webpage showcasing a collection of community districts through the power of JavaScript

As a beginner in javascript, I am working with a JSON file that contains an array of objects. Each object includes two properties: a borough and mappings. Mappings have two sub-properties called macro and neighborhoods. The macro represents a larger neighb ...

Sending information from popup to primary controller wit the use of AngularJS - (Plunker example provided) with an autocomplete field embedded in the popup

My scenario is a bit unique compared to passing regular data from a modal to the main controller. The input field in my modal has an autocomplete feature. Here is the Plunker I have included for reference: http://plnkr.co/edit/lpcg6pPSbspjkjmpaX1q?p=prev ...

Substitute all numerical values with a designated number from a variable

I came across a link that looks like this foo.net/index.php?page=15 My goal is to replace any number after page=xxx and retrieve the new number from a variable Currently, my code only replaces 15 with 16 var num = 16, // What if the str = foo.net/index ...

Tips for setting up a typeorm entity with attention to its nullable fields

How can I assign values to typeorm entities and insert them into the database? import { PricingPatternElement } from file const Element:PricingPatternElement = { displayOrder: 10, elementName: "test", createdAt : getCurrentDate(), createdBy: &qu ...

Change to the parent frame using webdriver

I am facing an issue while trying to switch to the parent frame or iFrame using webdriver.js. Although I have implemented a function that works, it only goes up to the second level of frames. When attempting to switch to the parent frame from a deeper fr ...