Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
131
rated 0 times [  135] [ 4]  / answers: 1 / hits: 18740  / 10 Years ago, wed, october 1, 2014, 12:00:00

I would like to get the a submit button using getElementsByTagName but I must be doing something wrong.



 <p id=ptag>Want to find out if this works</p>     

<button>The Button</button>

<form>
First name: <input type=text name=firstname><br>
Last name: <input type=text name=lastname><br>
<input type=checkbox name=yes value=yes>A checkbox<br>
<input type=submit value=Login>
</form>


in the above HTML snippet, If I use the below function, getting the button works yet getting the submit button does not.



 window.onload = theFunction;

function theFunction(){

document.getElementsByTagName(BUTTON)[0].setAttribute(onclick, theOtherFunction());

document.getElementsByTagName(INPUT)[3];.setAttribute(onclick, theOtherFunction());

}


What am I doing wrong?



 function theOtherFunction(){

document.getElementById(ptag).style.color = blue;

}


I added theOtherFunction() to my question because all my little experiment is doing is changing that <p> color to blue. With the button, it stays blue. With the submit button, it quickly turns blue and then goes back to black. What is the difference and how can I use the submit button to retain changes?


More From » html

 Answers
5

Take a look at your last line of code document.getElementsByTagName(INPUT)[3];.setAttribute(onclick, theOtherFunction());



There is a ; you don't want there right after the [3].
Hope this helps.


[#69273] Monday, September 29, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yosefleod

Total Points: 113
Total Questions: 100
Total Answers: 115

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
;