最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dom events - Javascript getElementbyID with button - Stack Overflow

programmeradmin3浏览0评论

EDIT: I have fixed the semi-colons, the case sensitivity, as well as the brackets. The code WORKS if I remove the functions after buttonPARTICULAR! Why?

EDIT: Fixed. I'm a dumbass. Sorry!!! :-Z

When I keep it simple, like this, everything works fine:

  <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, whenever I add more functions to the script, ALL of the buttons and code stop working.

 <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>

How is it that augmenting the quantity of virtually identical code will disrupt even the code that, before, was functional? Am I mitting a syntax error here?

EDIT

@Woot4Moot Still not functional

still not functional.

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 be more specific, when I load this code into my browser (the buttons and p id="desc") but when the buttons are clicked on, the p id="desc" does not change according to the JavaScript mand.

EDIT: I have fixed the semi-colons, the case sensitivity, as well as the brackets. The code WORKS if I remove the functions after buttonPARTICULAR! Why?

EDIT: Fixed. I'm a dumbass. Sorry!!! :-Z

When I keep it simple, like this, everything works fine:

  <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, whenever I add more functions to the script, ALL of the buttons and code stop working.

 <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>

How is it that augmenting the quantity of virtually identical code will disrupt even the code that, before, was functional? Am I mitting a syntax error here?

EDIT

@Woot4Moot Still not functional

still not functional.

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 be more specific, when I load this code into my browser (the buttons and p id="desc") but when the buttons are clicked on, the p id="desc" does not change according to the JavaScript mand.

Share Improve this question edited Mar 1, 2023 at 19:31 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 14, 2013 at 15:38 AndrewAndrew 7472 gold badges8 silver badges24 bronze badges 1
  • tags should be updated to reflect the more general javascript tag – Woot4Moo Commented Jan 14, 2013 at 15:44
Add a ment  | 

3 Answers 3

Reset to default 1

Besides the syntax errors:

function buttonINTENSIVOS()
}
document.getElementbyID("desc").innerHTML="Intensivo"
{
function buttonIMERSIVOS()
}
document.getElementbyID("desc").innerHTML="Imersivos"
{
function buttonPALESTRAS()
}

Your braces are the wrong way. Functions open with { not } and you dont have semicolons ; to terminate each mand

What you want is this:

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

As well as what has already been pointed out (incorrect opening/closing braces), javascript is case sensitive, so your method name is incorrect:

document.getElementbyID("...")

should be

document.getElementById("....")

(note the lower-case d in Id and the uppercase B in By)

Just to make this neater and easier to debug, I would suggest using the following function to condense the individual functions into one:

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

Then in your HTML:

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

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

Note the single quotes inside each of the brackets. The function will directly print the contents of the brackets to the p id=desc.

发布评论

评论列表(0)

  1. 暂无评论