Results 1 to 2 of 2

Thread: how to concatenate the above HTML code

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    782

    Question 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

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,714

    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/
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width