?
Solved

jQuery find and contains

Posted on 2014-12-10
7
Medium Priority
?
157 Views
Last Modified: 2014-12-10
Hello Experts,

I am trying to:

1. Find the TD in a table that its text is equal to the "loftNo" variable.
2. Then go to the next TD and grab its text.

Help! Today my brain doesn't work!!!!


$('area').each(function() {
     var loftNo = $(this).attr("id");
    var LoftType = $("td:first:contains("+loftNo+")").next().text();

Open in new window

0
Comment
Question by:Refael
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 57

Expert Comment

by:HainKurt
ID: 40491964
try this:

$("table td[text='" +leftNo + "']").next().text()
0
 
LVL 57

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 40491981
here is a working sample, giving "b"

<table>
    <tr><td>test</td><td>001</td><td>a</td></tr>
    <tr><td>test</td><td>002</td><td>b</td></tr>
    <tr><td>test</td><td>003</td><td>c</td></tr>
 <table>

...

var leftNo = "002";
alert($("table td").filter(function() { return $.text([this]) == leftNo; }).next().text());

Open in new window

0
 
LVL 57

Expert Comment

by:HainKurt
ID: 40491990
just make sure you pass unique values... if you pass "002" and we have 2 td, texts are joined :)
if i change my 3rd row td value to "002" and try that code, I will get "bc"
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Refael
ID: 40492005
Hi Huseyin KAHRAMAN :-) Thank you.

I think the problem was that i was searching on the first TD only. So I have changed it to "nth-child" and it looks that it is working fine. Yes filter would work better when dealing with duplicate values.
 $("td:nth-child(1):contains("+loftNo+")").next().text(); 

Open in new window

0
 

Author Closing Comment

by:Refael
ID: 40492008
Thank you Huseyin KAHRAMAN :-))
0
 
LVL 57

Expert Comment

by:HainKurt
ID: 40492009
oops, my nick should be "HainKurt" :) I made a mistake and put my f&l name somewhere...

fixed it by removing First & Last Name on profile page :)
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40492028
please provide a link to your website
0

Featured Post

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question