2

I have a dynamically created HTML string (a table rows). That rows count in that string can be greater than 1.

string myHtml = "
            <tr>
               <td class="text-left">
                  <label class="label-none">a.docx</label>
               </td>
               <td>
                  <label class="label-none">Manuscript </label>
               </td>
               <td>
                   <input class="del-file-cb" file-id="71" type="checkbox" />
               </td>
            </tr>" 
            etc...

Before inserting it to the DOM element (table) I want to check if inside it exists a checkbox input with the specific value of the file-id attribute. If so, I want to change the text of the 1st label (in the row where that found checkbox is placed) to e.g. lorem, and the 2nd label's text to e.g. ipsum. How to do that ?

Edit

also here are some useful informations stackoverflow.com/questions/704679/parse-html-string-with-jquery

Community
  • 1
  • 1
Tony
  • 12,405
  • 36
  • 126
  • 226

2 Answers2

9
var mYdomelement = $('
            <tr>
               <td class="text-left">
                  <label class="label-none">a.docx</label>
               </td>
               <td>
                  <label class="label-none">Manuscript </label>
               </td>
               <td>
                   <input class="del-file-cb" file-id="71" type="checkbox" />
               </td>
            </tr>');

if(mYdomelement.find(":checkbox").attr("file-id") == "Yourvalue"){
   $("label:first-child", mYdomelement).text('lorem');
   $("label:nth-child(2)", mYdomelement).text('ipsum');
}

Here is working demo

Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
-1

You can do something like this:

if($(#elementId + ' input[file-id|="YOUR_VALUE_HERE"]').length > 0)
{
    $(this).ancestors().eq(2).find('label').eq(0).val(FIRST_LABEL_VALUE_HERE);
    $(this).ancestors().eq(2).find('label').eq(0).val(SECOND_LABEL_VALUE_HERE);
}
neb
  • 1
  • 1
  • 4