javascript dynamically changing content of a web page with nested for loops
up vote
1
down vote
favorite
simply put i want to change all the values in headings to 't' when a button is pressed so i have the following code there are no errors
what i expect to happen is for all headings to change their content to 't'
window.onload = function ()
let specialButton = document.getElementById('mainButton')
specialButton.addEventListener('click', removeHeadings())
function removeHeadings ()
for (var j = 1, length = 7; j < length; j++)
let headings = document.getElementsByName('h' + j)
for (var i = 0, len = headings.length; i < len; i++)
headings[i].innerHTML = 't '
<h1>s</h1>
<h2>s</h2>
<h3>s</h3>
<button id="mainButton">Remove Headings</button>
also please tell me why the above does not work
javascript dom
add a comment |
up vote
1
down vote
favorite
simply put i want to change all the values in headings to 't' when a button is pressed so i have the following code there are no errors
what i expect to happen is for all headings to change their content to 't'
window.onload = function ()
let specialButton = document.getElementById('mainButton')
specialButton.addEventListener('click', removeHeadings())
function removeHeadings ()
for (var j = 1, length = 7; j < length; j++)
let headings = document.getElementsByName('h' + j)
for (var i = 0, len = headings.length; i < len; i++)
headings[i].innerHTML = 't '
<h1>s</h1>
<h2>s</h2>
<h3>s</h3>
<button id="mainButton">Remove Headings</button>
also please tell me why the above does not work
javascript dom
2
You wantgetElementsByTagName
– Robin Zigmond
Nov 10 at 18:56
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
simply put i want to change all the values in headings to 't' when a button is pressed so i have the following code there are no errors
what i expect to happen is for all headings to change their content to 't'
window.onload = function ()
let specialButton = document.getElementById('mainButton')
specialButton.addEventListener('click', removeHeadings())
function removeHeadings ()
for (var j = 1, length = 7; j < length; j++)
let headings = document.getElementsByName('h' + j)
for (var i = 0, len = headings.length; i < len; i++)
headings[i].innerHTML = 't '
<h1>s</h1>
<h2>s</h2>
<h3>s</h3>
<button id="mainButton">Remove Headings</button>
also please tell me why the above does not work
javascript dom
simply put i want to change all the values in headings to 't' when a button is pressed so i have the following code there are no errors
what i expect to happen is for all headings to change their content to 't'
window.onload = function ()
let specialButton = document.getElementById('mainButton')
specialButton.addEventListener('click', removeHeadings())
function removeHeadings ()
for (var j = 1, length = 7; j < length; j++)
let headings = document.getElementsByName('h' + j)
for (var i = 0, len = headings.length; i < len; i++)
headings[i].innerHTML = 't '
<h1>s</h1>
<h2>s</h2>
<h3>s</h3>
<button id="mainButton">Remove Headings</button>
also please tell me why the above does not work
window.onload = function ()
let specialButton = document.getElementById('mainButton')
specialButton.addEventListener('click', removeHeadings())
function removeHeadings ()
for (var j = 1, length = 7; j < length; j++)
let headings = document.getElementsByName('h' + j)
for (var i = 0, len = headings.length; i < len; i++)
headings[i].innerHTML = 't '
<h1>s</h1>
<h2>s</h2>
<h3>s</h3>
<button id="mainButton">Remove Headings</button>
window.onload = function ()
let specialButton = document.getElementById('mainButton')
specialButton.addEventListener('click', removeHeadings())
function removeHeadings ()
for (var j = 1, length = 7; j < length; j++)
let headings = document.getElementsByName('h' + j)
for (var i = 0, len = headings.length; i < len; i++)
headings[i].innerHTML = 't '
<h1>s</h1>
<h2>s</h2>
<h3>s</h3>
<button id="mainButton">Remove Headings</button>
javascript dom
javascript dom
edited Nov 10 at 18:55
Callam
7,68721522
7,68721522
asked Nov 10 at 18:52
TheLinuxPro
83
83
2
You wantgetElementsByTagName
– Robin Zigmond
Nov 10 at 18:56
add a comment |
2
You wantgetElementsByTagName
– Robin Zigmond
Nov 10 at 18:56
2
2
You want
getElementsByTagName
– Robin Zigmond
Nov 10 at 18:56
You want
getElementsByTagName
– Robin Zigmond
Nov 10 at 18:56
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
Firstly, you are executing the removeHeadings
function and passing it as the argument for the event listener. Secondly, you are trying to get elements by name when you mean to get them by their tag name – to do this use getElementsByTagName
.
window.onload = function ()
let specialButton = document.getElementById('mainButton')
specialButton.addEventListener('click', removeHeadings)
function removeHeadings ()
for (var j = 1, length = 7; j < length; j++)
let headings = document.getElementsByTagName('h' + j)
for (var i = 0, len = headings.length; i < len; i++)
headings[i].innerHTML = 't '
<h1>s</h1>
<h2>s</h2>
<h3>s</h3>
<button id="mainButton">Remove Headings</button>
whats the difference between getElementsByTagName and getElementsByName
– TheLinuxPro
Nov 10 at 19:05
Firstly, you are executing the removeHeadings function and passing it as the argument for the event listener. can you please clarify more
– TheLinuxPro
Nov 10 at 19:10
@TheLinuxPro The first one finds by Html TAGs and the second by the attributename
of elements.
– Ele
Nov 10 at 19:11
whats the difference between an html TAG and a name of an element isn't the name of the html element its tag
– TheLinuxPro
Nov 10 at 19:13
add a comment |
up vote
1
down vote
You're suddenly calling the function removeHeadings()
, you need to pass the function and not the result of calling it.
specialButton.addEventListener('click', removeHeadings()); // Remove the parentheses
^^
Further, you need to use the function getElementsByTagName
.
let headings = document.getElementsByName('h' + j)
^^^^^^^^^^^^^^^^^^^^
The function getElementsByName
finds elements by the attribute name
in elements. for Example:
<input type='button' name='myButton'>
^^^^
The function getElementsByTagName
finds elements by the HTML tag, in your case the H
tags, for example:
<input type='button' name='myButton'>
^^^^^
i am having difficulty understanding what you just said can you elaborate further
– TheLinuxPro
Nov 10 at 19:03
@TheLinuxPro see the updated answer
– Ele
Nov 10 at 19:14
i know i am bombarding you with questions but what do the parenthesis do whats the difference isn't the end result the same
– TheLinuxPro
Nov 10 at 19:21
@TheLinuxPro no, because you're calling the function and not passing it as a handler instead. What you're actually doing is passingundefined
which is the result of callingremoveHeadings
.
– Ele
Nov 10 at 19:26
add a comment |
up vote
0
down vote
By Using jQuery you can do this like.
HTML
<h1>S</h1>
<h2>S</h2>
<h3>S</h3>
<h4>S</h4>
<h5>S</h5>
<h6>S</h6>
<button id="change">CHANGE</button>
jQuery
$("#change").click(function change()
console.log("CLICKED");
$("h1, h2, h3, h4, h5, h6").html("T");
);
JsFiddle Live Example
isn't jquery redundant with most of the features of newer versions of javascript integrating jquery in?
– TheLinuxPro
Nov 10 at 19:19
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Firstly, you are executing the removeHeadings
function and passing it as the argument for the event listener. Secondly, you are trying to get elements by name when you mean to get them by their tag name – to do this use getElementsByTagName
.
window.onload = function ()
let specialButton = document.getElementById('mainButton')
specialButton.addEventListener('click', removeHeadings)
function removeHeadings ()
for (var j = 1, length = 7; j < length; j++)
let headings = document.getElementsByTagName('h' + j)
for (var i = 0, len = headings.length; i < len; i++)
headings[i].innerHTML = 't '
<h1>s</h1>
<h2>s</h2>
<h3>s</h3>
<button id="mainButton">Remove Headings</button>
whats the difference between getElementsByTagName and getElementsByName
– TheLinuxPro
Nov 10 at 19:05
Firstly, you are executing the removeHeadings function and passing it as the argument for the event listener. can you please clarify more
– TheLinuxPro
Nov 10 at 19:10
@TheLinuxPro The first one finds by Html TAGs and the second by the attributename
of elements.
– Ele
Nov 10 at 19:11
whats the difference between an html TAG and a name of an element isn't the name of the html element its tag
– TheLinuxPro
Nov 10 at 19:13
add a comment |
up vote
2
down vote
accepted
Firstly, you are executing the removeHeadings
function and passing it as the argument for the event listener. Secondly, you are trying to get elements by name when you mean to get them by their tag name – to do this use getElementsByTagName
.
window.onload = function ()
let specialButton = document.getElementById('mainButton')
specialButton.addEventListener('click', removeHeadings)
function removeHeadings ()
for (var j = 1, length = 7; j < length; j++)
let headings = document.getElementsByTagName('h' + j)
for (var i = 0, len = headings.length; i < len; i++)
headings[i].innerHTML = 't '
<h1>s</h1>
<h2>s</h2>
<h3>s</h3>
<button id="mainButton">Remove Headings</button>
whats the difference between getElementsByTagName and getElementsByName
– TheLinuxPro
Nov 10 at 19:05
Firstly, you are executing the removeHeadings function and passing it as the argument for the event listener. can you please clarify more
– TheLinuxPro
Nov 10 at 19:10
@TheLinuxPro The first one finds by Html TAGs and the second by the attributename
of elements.
– Ele
Nov 10 at 19:11
whats the difference between an html TAG and a name of an element isn't the name of the html element its tag
– TheLinuxPro
Nov 10 at 19:13
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Firstly, you are executing the removeHeadings
function and passing it as the argument for the event listener. Secondly, you are trying to get elements by name when you mean to get them by their tag name – to do this use getElementsByTagName
.
window.onload = function ()
let specialButton = document.getElementById('mainButton')
specialButton.addEventListener('click', removeHeadings)
function removeHeadings ()
for (var j = 1, length = 7; j < length; j++)
let headings = document.getElementsByTagName('h' + j)
for (var i = 0, len = headings.length; i < len; i++)
headings[i].innerHTML = 't '
<h1>s</h1>
<h2>s</h2>
<h3>s</h3>
<button id="mainButton">Remove Headings</button>
Firstly, you are executing the removeHeadings
function and passing it as the argument for the event listener. Secondly, you are trying to get elements by name when you mean to get them by their tag name – to do this use getElementsByTagName
.
window.onload = function ()
let specialButton = document.getElementById('mainButton')
specialButton.addEventListener('click', removeHeadings)
function removeHeadings ()
for (var j = 1, length = 7; j < length; j++)
let headings = document.getElementsByTagName('h' + j)
for (var i = 0, len = headings.length; i < len; i++)
headings[i].innerHTML = 't '
<h1>s</h1>
<h2>s</h2>
<h3>s</h3>
<button id="mainButton">Remove Headings</button>
window.onload = function ()
let specialButton = document.getElementById('mainButton')
specialButton.addEventListener('click', removeHeadings)
function removeHeadings ()
for (var j = 1, length = 7; j < length; j++)
let headings = document.getElementsByTagName('h' + j)
for (var i = 0, len = headings.length; i < len; i++)
headings[i].innerHTML = 't '
<h1>s</h1>
<h2>s</h2>
<h3>s</h3>
<button id="mainButton">Remove Headings</button>
window.onload = function ()
let specialButton = document.getElementById('mainButton')
specialButton.addEventListener('click', removeHeadings)
function removeHeadings ()
for (var j = 1, length = 7; j < length; j++)
let headings = document.getElementsByTagName('h' + j)
for (var i = 0, len = headings.length; i < len; i++)
headings[i].innerHTML = 't '
<h1>s</h1>
<h2>s</h2>
<h3>s</h3>
<button id="mainButton">Remove Headings</button>
answered Nov 10 at 18:57
Callam
7,68721522
7,68721522
whats the difference between getElementsByTagName and getElementsByName
– TheLinuxPro
Nov 10 at 19:05
Firstly, you are executing the removeHeadings function and passing it as the argument for the event listener. can you please clarify more
– TheLinuxPro
Nov 10 at 19:10
@TheLinuxPro The first one finds by Html TAGs and the second by the attributename
of elements.
– Ele
Nov 10 at 19:11
whats the difference between an html TAG and a name of an element isn't the name of the html element its tag
– TheLinuxPro
Nov 10 at 19:13
add a comment |
whats the difference between getElementsByTagName and getElementsByName
– TheLinuxPro
Nov 10 at 19:05
Firstly, you are executing the removeHeadings function and passing it as the argument for the event listener. can you please clarify more
– TheLinuxPro
Nov 10 at 19:10
@TheLinuxPro The first one finds by Html TAGs and the second by the attributename
of elements.
– Ele
Nov 10 at 19:11
whats the difference between an html TAG and a name of an element isn't the name of the html element its tag
– TheLinuxPro
Nov 10 at 19:13
whats the difference between getElementsByTagName and getElementsByName
– TheLinuxPro
Nov 10 at 19:05
whats the difference between getElementsByTagName and getElementsByName
– TheLinuxPro
Nov 10 at 19:05
Firstly, you are executing the removeHeadings function and passing it as the argument for the event listener. can you please clarify more
– TheLinuxPro
Nov 10 at 19:10
Firstly, you are executing the removeHeadings function and passing it as the argument for the event listener. can you please clarify more
– TheLinuxPro
Nov 10 at 19:10
@TheLinuxPro The first one finds by Html TAGs and the second by the attribute
name
of elements.– Ele
Nov 10 at 19:11
@TheLinuxPro The first one finds by Html TAGs and the second by the attribute
name
of elements.– Ele
Nov 10 at 19:11
whats the difference between an html TAG and a name of an element isn't the name of the html element its tag
– TheLinuxPro
Nov 10 at 19:13
whats the difference between an html TAG and a name of an element isn't the name of the html element its tag
– TheLinuxPro
Nov 10 at 19:13
add a comment |
up vote
1
down vote
You're suddenly calling the function removeHeadings()
, you need to pass the function and not the result of calling it.
specialButton.addEventListener('click', removeHeadings()); // Remove the parentheses
^^
Further, you need to use the function getElementsByTagName
.
let headings = document.getElementsByName('h' + j)
^^^^^^^^^^^^^^^^^^^^
The function getElementsByName
finds elements by the attribute name
in elements. for Example:
<input type='button' name='myButton'>
^^^^
The function getElementsByTagName
finds elements by the HTML tag, in your case the H
tags, for example:
<input type='button' name='myButton'>
^^^^^
i am having difficulty understanding what you just said can you elaborate further
– TheLinuxPro
Nov 10 at 19:03
@TheLinuxPro see the updated answer
– Ele
Nov 10 at 19:14
i know i am bombarding you with questions but what do the parenthesis do whats the difference isn't the end result the same
– TheLinuxPro
Nov 10 at 19:21
@TheLinuxPro no, because you're calling the function and not passing it as a handler instead. What you're actually doing is passingundefined
which is the result of callingremoveHeadings
.
– Ele
Nov 10 at 19:26
add a comment |
up vote
1
down vote
You're suddenly calling the function removeHeadings()
, you need to pass the function and not the result of calling it.
specialButton.addEventListener('click', removeHeadings()); // Remove the parentheses
^^
Further, you need to use the function getElementsByTagName
.
let headings = document.getElementsByName('h' + j)
^^^^^^^^^^^^^^^^^^^^
The function getElementsByName
finds elements by the attribute name
in elements. for Example:
<input type='button' name='myButton'>
^^^^
The function getElementsByTagName
finds elements by the HTML tag, in your case the H
tags, for example:
<input type='button' name='myButton'>
^^^^^
i am having difficulty understanding what you just said can you elaborate further
– TheLinuxPro
Nov 10 at 19:03
@TheLinuxPro see the updated answer
– Ele
Nov 10 at 19:14
i know i am bombarding you with questions but what do the parenthesis do whats the difference isn't the end result the same
– TheLinuxPro
Nov 10 at 19:21
@TheLinuxPro no, because you're calling the function and not passing it as a handler instead. What you're actually doing is passingundefined
which is the result of callingremoveHeadings
.
– Ele
Nov 10 at 19:26
add a comment |
up vote
1
down vote
up vote
1
down vote
You're suddenly calling the function removeHeadings()
, you need to pass the function and not the result of calling it.
specialButton.addEventListener('click', removeHeadings()); // Remove the parentheses
^^
Further, you need to use the function getElementsByTagName
.
let headings = document.getElementsByName('h' + j)
^^^^^^^^^^^^^^^^^^^^
The function getElementsByName
finds elements by the attribute name
in elements. for Example:
<input type='button' name='myButton'>
^^^^
The function getElementsByTagName
finds elements by the HTML tag, in your case the H
tags, for example:
<input type='button' name='myButton'>
^^^^^
You're suddenly calling the function removeHeadings()
, you need to pass the function and not the result of calling it.
specialButton.addEventListener('click', removeHeadings()); // Remove the parentheses
^^
Further, you need to use the function getElementsByTagName
.
let headings = document.getElementsByName('h' + j)
^^^^^^^^^^^^^^^^^^^^
The function getElementsByName
finds elements by the attribute name
in elements. for Example:
<input type='button' name='myButton'>
^^^^
The function getElementsByTagName
finds elements by the HTML tag, in your case the H
tags, for example:
<input type='button' name='myButton'>
^^^^^
edited Nov 10 at 19:15
answered Nov 10 at 18:57
Ele
22.2k42044
22.2k42044
i am having difficulty understanding what you just said can you elaborate further
– TheLinuxPro
Nov 10 at 19:03
@TheLinuxPro see the updated answer
– Ele
Nov 10 at 19:14
i know i am bombarding you with questions but what do the parenthesis do whats the difference isn't the end result the same
– TheLinuxPro
Nov 10 at 19:21
@TheLinuxPro no, because you're calling the function and not passing it as a handler instead. What you're actually doing is passingundefined
which is the result of callingremoveHeadings
.
– Ele
Nov 10 at 19:26
add a comment |
i am having difficulty understanding what you just said can you elaborate further
– TheLinuxPro
Nov 10 at 19:03
@TheLinuxPro see the updated answer
– Ele
Nov 10 at 19:14
i know i am bombarding you with questions but what do the parenthesis do whats the difference isn't the end result the same
– TheLinuxPro
Nov 10 at 19:21
@TheLinuxPro no, because you're calling the function and not passing it as a handler instead. What you're actually doing is passingundefined
which is the result of callingremoveHeadings
.
– Ele
Nov 10 at 19:26
i am having difficulty understanding what you just said can you elaborate further
– TheLinuxPro
Nov 10 at 19:03
i am having difficulty understanding what you just said can you elaborate further
– TheLinuxPro
Nov 10 at 19:03
@TheLinuxPro see the updated answer
– Ele
Nov 10 at 19:14
@TheLinuxPro see the updated answer
– Ele
Nov 10 at 19:14
i know i am bombarding you with questions but what do the parenthesis do whats the difference isn't the end result the same
– TheLinuxPro
Nov 10 at 19:21
i know i am bombarding you with questions but what do the parenthesis do whats the difference isn't the end result the same
– TheLinuxPro
Nov 10 at 19:21
@TheLinuxPro no, because you're calling the function and not passing it as a handler instead. What you're actually doing is passing
undefined
which is the result of calling removeHeadings
.– Ele
Nov 10 at 19:26
@TheLinuxPro no, because you're calling the function and not passing it as a handler instead. What you're actually doing is passing
undefined
which is the result of calling removeHeadings
.– Ele
Nov 10 at 19:26
add a comment |
up vote
0
down vote
By Using jQuery you can do this like.
HTML
<h1>S</h1>
<h2>S</h2>
<h3>S</h3>
<h4>S</h4>
<h5>S</h5>
<h6>S</h6>
<button id="change">CHANGE</button>
jQuery
$("#change").click(function change()
console.log("CLICKED");
$("h1, h2, h3, h4, h5, h6").html("T");
);
JsFiddle Live Example
isn't jquery redundant with most of the features of newer versions of javascript integrating jquery in?
– TheLinuxPro
Nov 10 at 19:19
add a comment |
up vote
0
down vote
By Using jQuery you can do this like.
HTML
<h1>S</h1>
<h2>S</h2>
<h3>S</h3>
<h4>S</h4>
<h5>S</h5>
<h6>S</h6>
<button id="change">CHANGE</button>
jQuery
$("#change").click(function change()
console.log("CLICKED");
$("h1, h2, h3, h4, h5, h6").html("T");
);
JsFiddle Live Example
isn't jquery redundant with most of the features of newer versions of javascript integrating jquery in?
– TheLinuxPro
Nov 10 at 19:19
add a comment |
up vote
0
down vote
up vote
0
down vote
By Using jQuery you can do this like.
HTML
<h1>S</h1>
<h2>S</h2>
<h3>S</h3>
<h4>S</h4>
<h5>S</h5>
<h6>S</h6>
<button id="change">CHANGE</button>
jQuery
$("#change").click(function change()
console.log("CLICKED");
$("h1, h2, h3, h4, h5, h6").html("T");
);
JsFiddle Live Example
By Using jQuery you can do this like.
HTML
<h1>S</h1>
<h2>S</h2>
<h3>S</h3>
<h4>S</h4>
<h5>S</h5>
<h6>S</h6>
<button id="change">CHANGE</button>
jQuery
$("#change").click(function change()
console.log("CLICKED");
$("h1, h2, h3, h4, h5, h6").html("T");
);
JsFiddle Live Example
answered Nov 10 at 19:16
Srinivasu Kotla
343
343
isn't jquery redundant with most of the features of newer versions of javascript integrating jquery in?
– TheLinuxPro
Nov 10 at 19:19
add a comment |
isn't jquery redundant with most of the features of newer versions of javascript integrating jquery in?
– TheLinuxPro
Nov 10 at 19:19
isn't jquery redundant with most of the features of newer versions of javascript integrating jquery in?
– TheLinuxPro
Nov 10 at 19:19
isn't jquery redundant with most of the features of newer versions of javascript integrating jquery in?
– TheLinuxPro
Nov 10 at 19:19
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242330%2fjavascript-dynamically-changing-content-of-a-web-page-with-nested-for-loops%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You want
getElementsByTagName
– Robin Zigmond
Nov 10 at 18:56