-
Jul 26th, 2021, 11:43 PM
#1
Thread Starter
Fanatic Member
how to concatenate the above HTML code
Hi,
I have an HTML like below:
Code:
`<input type="checkbox" class="editor-is_supervisor" onclick="myfunc(this)" id="${idx} " />`;
and I have a variable call 'status'
var status = 'checked' // the value can either 'checked' or empty
how to concatenate the above HTML code, I expect to get automatic checkbox checked, when the status value is 'checked', I tried below but it doesn't work, need help.
Code:
`<input type="checkbox" class="editor-is_supervisor" onclick="myfunc(this)" id="${idx} " + status />`;
thank you
-
Jul 28th, 2021, 10:17 AM
#2
Re: how to concatenate the above HTML code
It looks like you have back ticks surrounding the HTML with string interpolation which suggest to me that you are using JavaScript to dynamically create the DOM. If that is the case, then I would create the DOM a bit differently:
Code:
var input = document.createElement('input');
input.type = 'checkbox';
input.classList.add('editor-is_supervisor')
input.id = idx;
input.checked = (status === 'checked');
input.addEventListener('click', myfunc);
Fiddle: https://jsfiddle.net/jrzoshvw/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|