javascript/ jquery/ html

I need to get the Id of a div tag based on class div name?

 function AddActionTableRow() { actionTableRowCount++; actionItemRowDictionary[actionTableRowCount] = true; var rowHtml = "<tr class='actionTableRow actionTableRow" + actionTableRowCount + "'> <td class='actionTableField' id='actionTableField1'>" + actionTableRowCount + "</td> <td class='actionTableField' id='hiddenActionTableField'>" + 0 + "</td> <td><input class='actionTableField' id='actionTableField2' type='text'/></td> <td><select onchange='this.className=this.options[this.selectedIndex].className' class='actionTableField' id='actionTableField3'> <option value=' --Select-- ' selected disabled> --Select-- </option> <option class='blue' value='Category-1'>Category-1</option> <option class='green' value='In Progress'>Category-2</option> <option class='yellow' value='Minor Risk'>Category-3</option> <option class='red' value='Major Risk'>Category-4</option> </select></td> <td><select onchange='this.className=this.options[this.selectedIndex].className' class='actionTableField' id='actionTableField4'> <option value='--select--' selected disabled> --Select-- </option> <option class='blue' value='Completed'>Category-1</option> <option class='green' value='In Progress'>Category-2</option> <option class='yellow' value='Minor Risk'>Category-3</option> <option class='red' value='Major Risk'>Category-4</option> </select></td> <td><input class='actionTableField' id='actionTableField5' type='text'/></td> <td><input class='actionTableField' id='actionTableField6' type='text'/></td> <td><input class='actionTableField' id='actionTableField7' type='text'/></td> <td><input type='text' id='actionTableField8' class='actionTableField' /></td> <td><button onclick='DeleteActionItemRow(this)' class=' actionTableField actionItemDelete actionItemDelete-" + actionTableRowCount + " '><i class='fa fa-trash-o' style='border-radius:.30rem' ></i></button></td> </tr>"; $("#actionTableFoot").append(rowHtml); var className = ".actionTableRow" + actionTableRowCount + " #actionTableField2"; alert(className); $(className).focus(); alert("wdfs");; var picker = new Lightpick({ //field: document.getElementById('actionTableField8'), //field: document.getElementsByClassName('actionTableRow actionTableRow' + actionTableRowCount).getElementById('actionTableField8'), singleDate: false, onSelect: function (start, end) { var str = ''; str += start? start.format('Do MMMM YYYY') + ' to ': ''; str += end? end.format('Do MMMM YYYY'): '...'; document.getElementById('actionTableField8').innerHTML = str; } }); }
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

I think you want to get id of an element from class name, you can achive it by,

<div id="testId" class="testClass"></div>
$(document).ready(function() {
    alert($('.testClass').attr('id'));
}); 

But keep in mind class could be used at multiple places, so if you want id of all elements with same class name, you can

var retval = []
$('selector').each(function(){
  retval.push($(this).attr('id'))
})
return retval

or using map

return $('.selector').map(function(index,dom){return dom.id})

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question How to get div class name/id from dropped sortable element? How to get element without ID or Class Name using Javascript DOM How to get the specific name of an element based on attribute position in the class Get element by ID using class name JQuery changing class of element with id or class based on page url/name How to get element by class name How to get the class name of an element How to replace class name for element without ID? How to reference an element's id based on looping through elements by class name Get ID name of a clicked element and show divs with the same name of class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM