0

I am creating a table using tag. I'm able to catch the element but not able to convert it into a string and perform the conditional check. Please help!!

$(document).ready(function(){
    $('#user tr').each( function(){
           //add item to array
        var value = $(this).find(".user1").html();

        /********* want to do something like 

     if (value == "Y") {
       //apply a class
     }
     *************/

        });
1
  • 1
    What does the markup look like? What do you end up with in value when you try your commented-out code? Commented May 15, 2012 at 5:04

4 Answers 4

2
if (value === "Y") {
    $(this).find(".user1").addClass("a-class");
}
Sign up to request clarification or add additional context in comments.

2 Comments

it works under the <div> tag. when trying with .value it catches no value. The code on browser is:- <tbody> <tr id="myrowKEYSTATSBROKER" class="odd good"> <td class="user" style="width: 175px;" scope="request"> <td class="user1" style="width: 75px;" scope="request"> N </td> <td style="width: 175px;"> </td> <td style="width: 100px;">KEYSTATSBROKER</td> <td style="width: 90px;"> - </td> <td style="width: 175px;display: none;white-space: nowrap;"> </td> <td style="text-align: center; width: 50px;"> </td> <td style="text-align: center; width: 50px;">
.value is the property for getting the user-entered input out of <input type=..., wrapped by jQuery in .val(). .html() is jQuery's wrap of .innerHTML property -- the markup nested inside this item. When I run your sample, I get " N ".
1

If you need the html, the find returns a collection of results, try to produce a find query very specific and pick the first

var html = $(this).find(".user1").first().html();

But you can use addClass in this case, is much more effective.

1 Comment

.first().html() fetches me the value but i'm unable to perform any conditional check over the value I get
1

Use .get() method of jquery.

var value = $(this).find(".user1").get(0);

Now u can perform any condition check .

Comments

0

The html() method of jquery only grabs the inner html of an element. The best way to grab the actual html of an element would be to use

element.outerHTML;

to do that though you have to actually have be manipulating the dom element and not the jquery object.

for example you would do something like this

var html = $(this).find(".user1").get(0).outerHTML;

That will return ALL of the html as a string though, including its contents, not just its HTML of the tags. So if you just want the element HTML, I would clone the element and empty its HTML content.

var tag = $(this).find(".user1").clone().html('').get(0).outerHTML;

1 Comment

.outerHTML fetches no value in my case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.