Solved

jQuery find and contains

Posted on 2014-12-10
7
147 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
  • 4
  • 2
7 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 40491964
try this:

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

Accepted Solution

by:
HainKurt earned 500 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 51

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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 51

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
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)

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now