/    Sign up×
Community /Pin to ProfileBookmark

Concatenation confusion with quotes

In place of a series of statements like these below:

document.getElementById(‘testButton1’).disabled = false;
document.getElementById(‘testButton2’).disabled = false;
document.getElementById(‘testButton3’).disabled = false;

I want to subsitute a loop:

for(i=1;i<=3;i++){
document.getElementById( ).disabled = false;
}

What will go inside the ( ) to produce the correct id?
“i” being the number in each id name.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@ElvinasJul 25.2016 — paste this 'testButton'+i inside ()

When you want to concatenate variable to string you just add '+' after ending quote and if you want to concatenate another string you just type in quotes and repeat the same process.
Copy linkTweet thisAlerts:
@PittsburghRedauthorJul 25.2016 — Thank you. It worked, of course and I have 24 buttons to enable/disable.

I understand how to join strings and vars. What confuses me is how the quotes work. The single quotes around the text inside the ( ) in the original statement do not appear in the new statement.

document.getElementById('testButton'+i ).disabled = false.
Copy linkTweet thisAlerts:
@ElvinasJul 25.2016 — Thank you. It worked, of course and I have 24 buttons to enable/disable.

I understand how to join strings and vars. What confuses me is how the quotes work. The single quotes around the text inside the ( ) in the original statement do not appear in the new statement.

document.getElementById('testButton'+i ).disabled = false.[/QUOTE]


I would really like to help you, but I don't understand what do you mean by "how the quotes work".

Can you be more specific what you don't understand about single quotes when wrapped around text?
Copy linkTweet thisAlerts:
@PittsburghRedauthorJul 25.2016 — Thanks for being concerned.

Here is the original statement:

document.getElementById('testButton3').disabled = false;

Here is the statement with the var:

document.getElementById('testButton'+i).disabled = false;

The second statement does not have single quotes around the WHOLE id. Why not:

document.getElementById(' "testButton" +i').disabled = false;

The answer, of course, is it doesn't work.

I had some help a few days ago on the forum [maybe from you?] with this statement:

$("div.wrapper2").css('background-image', 'url(" '+bgimage +' ")');

The quotes boggled my mind. I am content to have a "model" and just plug in what I need.
Copy linkTweet thisAlerts:
@SempervivumJul 25.2016 — [CODE]document.getElementById(' "testButton" +i').disabled = false; [/CODE]In this code the variable i is not substituted as it's part of the string. The string simply results in "testButton" +i[/QUOTE]
while the parameter in
[CODE]document.getElementById("testButton" +i).disabled = false;[/CODE]
results in, e. g.:
testButton1[/QUOTE]
Copy linkTweet thisAlerts:
@rootJul 25.2016 — If you have a form with 24 buttons then you need to have others disabled, why are you not using a radio set of 24 radio buttons that only allows one option to be selected at any given time..?
Copy linkTweet thisAlerts:
@PittsburghRedauthorJul 25.2016 — I get it. In the original statement, the parameter is a string. In the second statement, the parameter is a variable and it's value produces the string.

I don't have a form. I'm building a little mobile web app and the buttons call a function, each with a different parameter. I have to think about the possibility of radio buttons working.

Thank you both for your interest. I work alone and appreciate the help.
Copy linkTweet thisAlerts:
@rootJul 26.2016 — If you are using form elements then you should use a form for simpler addressing and for your purpose as you describe your problem, radio buttons would solve most of them because of the generic way in which they work...
×

Success!

Help @PittsburghRed spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...