user1234
user1234

Reputation: 3159

How to write if statement to check for multiple text() values in jquery

I have a table column that performs a specific action depending on the text values. below os the html:

<td class="product"><span class="product">customer</span></td>
<td class="product"><span class="product">customer</span>, <span class="product">specifics</span></td>

<td class="product"><span class="product">admin</span>, <span class="product">specifics</span></td>

Now here, im trying to write a condition for certain text values. if the values are "customer and/or specifics", I'd like to create a dialog box, else if the values are "admin", I'd like to navigate to the main page. here is the js:

var rows = $(this).parents('tr');
if (rows.find('td.product').has('span.product').text() === 'customer') {

    alert('show dialog message');

} else {
    alert('navigate back to main page');
}

However, this code will only check for the values for "customer" (but obvious) and not for the remaining values. How can i check for all the text values and perform action accordingly? Any ideas?? Thanks

EDIT:

I just wanted to clarify few things: the text val can hold multiple values like: "customer, specifics,admin" for one row and it could also hold just "customer" or just "specifics". So if any of the text values contains the admin as a value, I'd redirect it to the main page..(the 2nd condition in my case). Only if the row contains values other than the admin, it should show a dialog message (1st condition in this case). Sorry for not being clear at first.

Upvotes: 0

Views: 782

Answers (2)

Giannis Grivas
Giannis Grivas

Reputation: 3412

Try that : http://jsfiddle.net/csdtesting/ocju73ox/4/

var toredirect = false;
var showdialog = false;
//test scenarios
//var val = "admin";//works perfect
//var val = "customer, specifics"; //works perfect
//var val = "admin, specifics"; // shouuld redirect to main page, but calls dialog
var val = "admin, customer";// shouuld redirect to main page, but calls dialog

//var val = "customer, specifics";//works perfect
//var val = "customer";//works perfect

//rows.find('td.product').has('span.product').text();
// considering the text value is "admin, specifics" , 3rd <td>
alert(val);
if (checkif(val, "customer") || checkif(val, "specifics")) {
    showdialog = true;
}
if (checkif(val, "admin"))  {
   toredirect = true;
}

/*alert(toredirect);
alert(showdialog);*/

if (toredirect) {
    //redirect
    alert("will redirect");
    return false;
} else if (showdialog) {
    alert("will show dialog");
    //showdialog
}

function checkif(input, word) {
    if (input.indexOf(word) > -1) {       
        return true;     
    } else {
        return false;
    }

Revision asked 1: http://jsfiddle.net/csdtesting/th3Lazfj/2/

Revision asked 2: http://jsfiddle.net/csdtesting/th3Lazfj/4/

Upvotes: 1

Hitesh
Hitesh

Reputation: 4288

Try this : http://jsfiddle.net/hiteshbhilai2010/2nd215c3/6/

var rows = $(this).parents('tr');
var arrClass = ["product","product","product","product"];
if (rows.find('td.product').has('span.product').text() === 'customer')
               {

               alert('show dialog message');

               }else{
               alert('navigate back to main page');}
//try this 
jQuery("span.product").each(function(){
$(this).text();

    if ($(this).text() === 'customer')
               {

               alert('show dialog message');

               }else{
               alert('navigate back to main page');}

})

Upvotes: 0

Related Questions