UnKnown UnKnown - 25 days ago 7
jQuery Question

How check if a video-id exists inside the table or not

I have a table as shown below , I am trying to check if a video-id exists inside the table or not

<div class="portlet-body">
<table class="mytable1 table table-bordered table-hover" id="videosfromtagstable">
<tbody class="connectedSortable ui-sortable">
<tr class="existingvideos">
<th>Name</th>
<th>Video</th>
</tr>
<tr video-id="6" class="newvideos" style="display: table-row;">
<td>One</td>
<td><a href="xxx" target="_blank">xxx.jpg</a></td>
</tr>
<tr video-id="48" class="newvideos">
<td>Two</td>
<td><a href="xxx" target="_blank">xxx.jpg</a></td>
</tr>

</tbody>
</table>


I am checking it this way , this is working, but could you please let me know if this the correct and efficient procedure ??

var checkvalue = checkifVideoExists(48);

alert(checkvalue);

function checkifVideoExists(videoid)
{
var flag = false;
$('#videosfromtagstable > tbody > tr').each(function()
{
if ($(this).attr('video-id') == videoid)
{
flag = true;
return false;
}
else
{
return true;
}
});
return flag;
}


http://jsfiddle.net/e08o7uct/37/

Answer

It's not efficient way. Better select element via it's attribute:

function checkifVideoExists (videoid) {
    return $('#videosfromtagstable [video-id="'+videoid+'"]').length > 0;
}
Comments