user2625007
user2625007

Reputation: 59

using a javascript variable to refer to an element with id containing the variable

HTML In my html I have an element with id ="check1"

<span id="check1"></span>

JAVASCRPIT I have a variable a='1', which is coming from somewhere else. Now, I want the inner HTML of span element to be "correct" with the help of reference to its id, and using 'a'.

var a = 1;
$.(#**checka**).innerHTML="correct"

How to write the id ??

Upvotes: 1

Views: 1856

Answers (3)

Paul Rad
Paul Rad

Reputation: 4882

$('#check' + a).html('correct');

or

$('#check' + a)[0].innerHTML = ('correct');

or

document.getElementById('check' + a).innerHTML = ('correct');

Upvotes: 1

Labib Ismaiel
Labib Ismaiel

Reputation: 1340

first notice that innerHTML() is javascript and the jquery function is html() and always remember that you can use concatenation everywhere. with that said:

$('#check' + a).html('correct');

Upvotes: 0

Vahid Hallaji
Vahid Hallaji

Reputation: 7447

JS:

document.getElementById("check" + a).innerHTML = "correct";

Jquery:

$("#check" + a).html("correct");

Upvotes: 2

Related Questions