1

I have the following table structure:

<table class=ms-listviewtable>
  <tr class="ms-itmhover">
    <td class="ms-vb2">Value1</td>
    <td class="ms-vb2">Value2</td>
    <td class="ms-vb2">Value3</td>
    <td class="ms-vb2 ms-lastCell">Value4</td>
  </tr>
</table>

I would like to be able to change the value of the last column which is currently "Value4" to "changedValue" for example using jQuery;

I have written the following but it does not seems to be working at all, not really sure where I am going wrong:

$(document).ready(function() {
  $('.ms-vb2 .ms-lastCell').each(function() {
    var lastColumn = $(this).html();
    var replaceValue = lastColumn.Append("Changed Content");

    $(this).html(replaceContent);
  });
});

Any suggestions will be greatly appreciated.

1
  • 1
    you forget a quote! $('.ms-vb2 .ms-lastCell') Commented Mar 6, 2012 at 11:39

6 Answers 6

3

I understand that you want to append text to the last td. Here is the code for that

jQuery('.ms-lastCell').each(function() {

 var lastColumn = $(this).html();
 var replaceValue = lastColumn + "Changed Content";

jQuery(this).html(replaceValue );


});

http://jsfiddle.net/G3eYh/13/

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

2 Comments

Thanks I have tried this, but it does not replace the value of the last column. I have removed the "lastColumn +" and left the string value, this seemed to do the trick.
oh sorry.. i thought that u want to append the text so used "+" to concat present td value with ur text and replaced.
2

First of all, your selector is incorrect:

$('.ms-vb2.ms-lastCell')

As both classes belong to one td, you must place them without any space. With space this means there is an element class ms-vb2 and within this element there is another element with class ms-lastCell.

You migh want also to use .text() instead of html() and a simple javascript string concatination:

var lastColumn = $(this).text();
var replaceValue = lastColumn + "Changed Content";
$(this).text(replaceContent);

Comments

2

You have a space separating your class names in the selector where there doesn't need to be one, and you just need to change the text not the html.

See jsFiddle here

Comments

1

There are three problems with the following line:

$('.ms-vb2 .ms-lastCell).eachl(function() {
  1. You didn't put a closing ' after lastCell.
  2. The space in your selector means you will be selecting any .ms-lastCell elements that are descendants of .ms-vb2 elements. If you remove the space and say '.ms-vb2.ms-lastCell' it will find elements with both classes.
  3. You misspelled .each (with an l on the end).

There are a couple of problems with the contents of your .each() callback. The first line is OK, it gets the contents of the cell. But then:

var replaceValue = lastColumn.Append("Changed Content");

I'm not sure what you're trying to do here, but lastColumn is a string, and strings don't have an Append() method. If you want to add to the existing value you can say:

var replaceValue = lastColumn + "Changed Content";

If you just want to replace it say:

var replaceValue = "Changed Content";

If you intend to throw away the old value and just provide a new value you can skip all of that and say:

$(this).html("Changed Content");

Finally, you don't really need to use .each() when only one cell is going to match your selector (or when you want to update all matching elements to the same value), so you can just do this:

 $('.ms-vb2.ms-lastCell').html("Changed Content");

Or if you want to add to the existing content you can use the syntax of the .html() method that takes a callback function:

 $('.ms-vb2.ms-lastCell').html(function(i, oldHtml) {
     return oldHtml + "Changed Content";
 });

Where jQuery calls your callback function passing in the old value so you can process it however you see fit and then the value you return becomes the new html content of the element. If more than one element matches your selector your callback will be called for each in turn.

Comments

0

There are quite a few errors in your html and javascript but try this:

$(document).ready(function() {

    $('.ms-vb2', '.ms-listviewtable').each(function() {

        $(this).html("Changed Content");

    });
});

Comments

0
$(document).ready(function() {
    $('.ms-lastCell').each(function() {
        var lastColumn = $(this).html(); 
        var replaceValue = lastColumn.Append("Changed Content");
        $(this).html(replaceContent);
    });
});

I think you are using this jquery code in the head part of the page. please use this code at the end of the page before </body> tag and then it will work.

3 Comments

document ready makes sure the code gets executed when dom is completly loaded, so no need to put it in the end. There might be performance issues, but working code also runs with document ready.
@Boo : you are right but my personal experience is, some time it gives an error if we are putting document .ready in <head> tag and we are using any page element id or class at there. and you see that in many jquery controls the sample examples shows document ready function at the end of the file.
If you're putting the script at the end of the body you don't need document.ready at all.

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.