1

I have a table. When I click on(within) a TD I need to show a hidden div that contains several more divs inside. Each div inside that hidden container has a text value. I need to select the one which value corresponds to the clicked TD.

JS

$(".clickme").click(function(){
    $("#hiddenDiv").hide().fadeIn(200);

    if ($(this).text() == $("#hiddenDiv div").text()) {
         //  HOW DO I SELECT THAT DIV? 
         // matched div .css("color", "red");  
    }

});

HTML

<table id="myTbl">
<tr>
  <td></td>
  <td class="clickme">Left</td>  
</tr>
</table>

<div id="hiddenDiv" style="display:none">
  <div>Straight</div>
  <div>Left</div>
  <div>Right</div>
</div>
santa
  • 11,716
  • 43
  • 149
  • 239

3 Answers3

3

Use :contains to select the correct one:

$("#hiddenDiv div:contains(" + $(this).text() + ")")
McGarnagle
  • 98,751
  • 30
  • 222
  • 258
1

demo jsBIn

$(".clickme").click(function(){

    var thisText = $(this).text();
    var $targetEl =  $('#hiddenDiv > div:contains('+thisText+')');

    if( $targetEl.length > 0 ){  // if exists
          $("#hiddenDiv").hide().fadeIn(200);
         $targetEl.css({color:'red'});
    }

});
Roko C. Buljan
  • 180,066
  • 36
  • 283
  • 292
0
$(".clickme").click(function() {
    $("#hiddenDiv").hide().fadeIn(200);
    var $this = $(this);
    $("#hiddenDiv div").filter(function() {
        return $(this).text() == $this.text();
    }).css("color", "red").show();
});​
gdoron is supporting Monica
  • 142,542
  • 55
  • 282
  • 355