javascript - HTML id tag conflict with array member -
my page has bunch of id's on <div>
elements - xx1, xx2, xx3, xx4
.
i have array, idindex = [xx1, xx2, xx3, xx4]
, used construct id tag using jquery, follows:
$("#" + idindex[2]).text("new text here");
unfortunately, not work. javascript dereference actual id in page instead of constructing tag, , tells me idindex[2]
[object htmldivelement]
, jquery command not work.
how build name of id tag?
if have div id of xx1
, there global variable called xx1
element in question.
when write idindex = [xx1]
, you're building array of div elements. if want build array of string ids, need idindex = ["xx1"]
etc. quotes important.
that said, already have array of elements. instead of re-selecting element dom id, wrap element in jquery object:
var $el = $(idindex[2]);
by way of explanation, here's single div id of xx1
. can see there xx1
variable , wrapping in $()
works fine:
console.log(xx1); $(xx1).text("test 123")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="xx1"></div>
Comments
Post a Comment