0

I am trying to combine the words in td and input text entered in the textbox.

<table width="100%">
<tr>
<td class="CommentWords" width="25%">jsakldjs </td>
  <td class="Label" width="85%"> <input class="Textcomment"
      id="txtCommentWords_3" type="text" width="550px">
</td>
</tr>

<tr class="NormalItem">
    <td class="CommentWords" width="25%">klsajdksajd </td>
    <td class="Label" width="85%"> <input class="Textcomment"
        id="txtCommentWords_4" type="text" width="550px"> </td>
</tr>

</table>

as i need to select all words from "commentWords" and "TextComment class", with associated values. Fiddler link.

for eg:

output will be

jsakldjs Å InputTextvalue ¶ // Å will be Field separator, ¶ will be Row Seperator

this what i tried, but as of now it wil return either textbox value or td value. i need to get all td value with corrsponding text values with field and row separator

var list = $(".CommentWords, .Textcomment", this).map(function() {
                            return $(this).val();
                        });

2 Answers 2

2

You should do something like

var list = $(".CommentWords, .Textcomment", $('#table')).map(function() {
    var $this = $(this);
    if ($this.is('td')) {
        return $this.text();
    } else {
        return $this.val();
    }
});

Print the results

for(i = 0; i < list.length; i++){
    var current  = list[i];
    $('#result').append(current);
    if(i % 2 !== 0){
        $('#result').append(' [end of line] ');
    }else{
         $('#result').append(' [td - input separetor] ');
    }
}

if you need to conactenate a string

 var finalString = '';
 for(i = 0; i < list.length; i++){
    var current  = list[i];
    finalString  += current;
    if(i % 2 !== 0){
        finalString += ' [end of line] ';
    }else{
        finalString += ' [td - input separetor] ';
    }
 }

fiddle here http://jsfiddle.net/23hgy/2/

Sign up to request clarification or add additional context in comments.

2 Comments

i need to join with field and row seperator
@Ravi i added that, look at the fiddle, i also added a method to concatenate in a string
2

The text of the <td> is retrieved using the text() function. I made a fiddle to show you:

http://jsfiddle.net/U9xnj/1/

basically, you can do this, but it is not very readable:

var list = $(".CommentWords, .Textcomment").map(function() {
                            return $(this).text() || $(this).val();
                        });

4 Comments

Both answers are good, but yours is much nicer. Just in style of jQuery
i need to join with field and row seperator wich i mentioned above
@Ravi Just use var joined = list.join("your separator")
@Oybek no he needs to join in a different way

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.