最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to chk if span tag contain text or not - Stack Overflow

programmeradmin1浏览0评论

see fiddle if patient has time slot alloted already then make color yellow using jquery(client side). in my fiddle if i allot time slot first time then color is green and at the same time when allot next time slot then previous time slot is grey. i have to make it yellow. for that i have to find span tag in table and check wheather contains text or not if contains text then make it yellow.But i m new in jquery.i search lot but not getting. how can i do that.

//This is button click code

  $("#<%=btnSelect.ClientID%>").click(function ()           {


                var cells = $('#tableAppointment tbody tr td:nth-child(3)');


                var i = 0;
                var topcell = false;

                cells.each(function ()
                {
                    var $cell = $(this);

                    if ($cell.hasClass('csstdhighlight'))
                    {
                        if (i == 0)
                        {
                            $cell.find('span').text(jQuery('#<%=txtPatientName.ClientID%>').val());
                            topcell = $cell;
                        }
                        else
                        {
                            $cell.remove();

                        }
                        i++;
                    }
                });
                if (topcell && i > 1) topcell.attr('rowspan', i);
                $("#tableAppointment tr").each(function ()
                {

                    $('td', this).each(function ()
                    {
                        var value = $(this).find("span").val();

                        if (!value)//i m chking like this
                        {

                        }
                        else
                        { //make it yellow                      
                        }
                    })

                })

                return false;
            });
        });

see fiddle if patient has time slot alloted already then make color yellow using jquery(client side). in my fiddle if i allot time slot first time then color is green and at the same time when allot next time slot then previous time slot is grey. i have to make it yellow. for that i have to find span tag in table and check wheather contains text or not if contains text then make it yellow.But i m new in jquery.i search lot but not getting. how can i do that.

//This is button click code

  $("#<%=btnSelect.ClientID%>").click(function ()           {


                var cells = $('#tableAppointment tbody tr td:nth-child(3)');


                var i = 0;
                var topcell = false;

                cells.each(function ()
                {
                    var $cell = $(this);

                    if ($cell.hasClass('csstdhighlight'))
                    {
                        if (i == 0)
                        {
                            $cell.find('span').text(jQuery('#<%=txtPatientName.ClientID%>').val());
                            topcell = $cell;
                        }
                        else
                        {
                            $cell.remove();

                        }
                        i++;
                    }
                });
                if (topcell && i > 1) topcell.attr('rowspan', i);
                $("#tableAppointment tr").each(function ()
                {

                    $('td', this).each(function ()
                    {
                        var value = $(this).find("span").val();

                        if (!value)//i m chking like this
                        {

                        }
                        else
                        { //make it yellow                      
                        }
                    })

                })

                return false;
            });
        });
Share Improve this question edited Jun 14, 2012 at 7:48 Vladimir Starkov 19.8k8 gold badges62 silver badges115 bronze badges asked Jun 14, 2012 at 7:34 Nikhil DNikhil D 2,5093 gold badges22 silver badges41 bronze badges 6
  • $('#tableAppointment tbody tr td:nth-child(3)') returns only one element and using each is redundant. – Ram Commented Jun 14, 2012 at 7:42
  • console.log is undefined – Nikhil D Commented Jun 14, 2012 at 8:15
  • no, console.log() is not undefined. – Ram Commented Jun 14, 2012 at 8:29
  • @undefined if i allot time slot first time then color is green and at the same time when allot next time slot then previous time slot make yellow using jquery any other logic – Nikhil D Commented Jun 14, 2012 at 9:30
  • @nikhil if you have any problem still then come to this room chat.stackoverflow.com/rooms/12535/anonmous – user1432124 Commented Jun 14, 2012 at 9:48
 |  Show 1 more comment

4 Answers 4

Reset to default 6
if($('span').text().length == 0){
  console.log('No Text');
}
else{
  console.log('Has Text');
}

Demo: http://jsfiddle.net/hj4Bt/1/

Use $(this).find("span").text() instead. Check if the returned string is empty.

Instead of this

var value = $(this).find("span").val();

write

var value = $(this).find("span").text();

and if you want to check html inside span then use

var value = $(this).find("span").html();

UPDATE :-

<html><head>
<style>.yell{background:#CCFF00;}.csstd
{
background-color: #ccffcc;
}
table{
background-color: #cccccc;

}</style><script src="jquery.js"></script>
<script>var is=false;
$(function(){

$(document).mouseup(function(){is=false;});
$(".csstablelisttd").mousedown(function(e){sen=$(this);
$(".csstd").removeClass("csstd");
  $(this).children(":not(:first)").addClass("csstd");
  xco=e.clientX;
  yco=e.clientY;
  is=true;
  return false;
});
document.onmousemove=function(){return false;};
$(".csstablelisttd").mouseenter(function(){
if(is){
  $(this).children(":not(:first)").addClass("csstd");
if($(".csstd").length>=6){is=false;}
}
});
$("#update").click(function(){
if($("#names").val())
{
$(".csstd").next().hide();

$(".csstd:first").next().show().attr("rowspan",$(".csstd").length/2).children().html($("#names").val());
$(".csstd").removeClass("csstd").addClass("yell");
}
});
 });</script></head><body>
<table id="tableAppointment" cellspacing="1" width="50%" border="1" align="center">
<thead>
<tr class="csstablelisttd">
<td bgcolor="#ffffff">
</td>
<td >
</td>
<td class="csstablelisttd patientName">
<b>Patient Name</b>
</td>
</tr>
</thead>
<tbody>
<tr class="csstablelisttd">
<td  valign="top" width="70px">
8:00AM
</td>
<td >
0
</td>
<td >
<span></span>
</td>
</tr>
<tr class="csstablelisttd">
<td >
</td>
<td >
15
</td>
<td >
<span></span>
</td>
</tr>
<tr class="csstablelisttd">
<td >
</td>
<td >
30
</td>
<td >
<span></span>
</td>
</tr>
<tr class="csstablelisttd">
<td >
</td>
<td >
45
</td>
<td >
<span></span>
</td>
</tr>
<tr class="csstablelisttd">
<td  valign="top" width="90px">
9:00AM
</td>
<td >
0
</td>
<td >
<span></span>
</td>
</tr>
<tr class="csstablelisttd">
<td >
</td>
<td >
15
</td>
<td >
<span></span>
</td>
</tr>
<tr class="csstablelisttd">
<td >
</td>
<td >
30
</td>
<td >
<span></span>
</td>
</tr>
<tr class="csstablelisttd">
<td >
</td>
<td >
45
</td>
<td >
<span></span>
</td>
</tr>
</tbody>
</table>
<input type="text" id="names" />
<input type="button" id="update" value="update" /> </body></html>

Is that what you want?

See demo: http://jsfiddle.net/FDEep/3/

$('input').click(function(){
    // All <span> inside <td>
    $('td span').each(function(index){
        // Check if 'span' is empty
        if( $(this).text().length !== 0 ){
            // Change the bg color of the 'span' not empty
            $(this).parent().css('background-color', 'yellow');
            // When a span with some value is found, alert the index of the row
            alert( $(this).closest('tr')[0].rowIndex);
        }
    });
});

Adapt at your will

发布评论

评论列表(0)

  1. 暂无评论