views:

52

answers:

4

I have asp.net repeater on a page. If each item being repeated is wrapped in a label like so:

<label class="ItemName">value</label>

If this label contains the text '35' I want to display some text next to it. How can i do this using jquery???

    jQuery(document).ready(function () {
        if ($('.ItemName').val().indexOf("35")) {
            $(this).val() = $(this).val() + "some text";
        }
    });
A: 

indexOf returns -1 if not found, or the index. Do this:

if ($('.ItemName').val().indexOf("35") >= 0) {
jtbandes
A: 

.text() should work:

var item = $('.ItemName');
if ( item.text().indexOf("35") > -1 ) {
    item.after("some text");
}
J-P
i dont want all the labels to have the text after just the ones that have that text. I think here its detecting whether any have that text/
phil crowe
A: 

this you mean?

jQuery(document).ready(function () {
        if ($('.ItemName').text().indexOf("35")) {
            $(this).text($(this).text() + "some text");
        }
    });
Mark Baijens
when i try this
phil crowe
+4  A: 
  1. The this in the .ready function should refer to the document.
  2. To get the text content, use .text() instead of .val().
  3. To update some value, use $obj.val(blah);, not $obj.val() = blah;. (This is actually a limitation of Javascript.)
  4. There is a :contains() selector to filter elements containing some text.
  5. To append some text (or HTML), there is already an .append() method (Thanks @J-P for reminding this.)

You may want this instead:

$('.ItemName:contains(35)').append("some text");
KennyTM
Very jQuery'ish! ;) ... what's wrong with `$(this).append()` (probably a tad faster than using the fancy `.text()` callback)?
J-P
@Kenny - the `x` is the index in your example, should be: `$('.ItemName:contains(35)').text(function(i, x) { return x + "some text"; });`
Nick Craver
@J-P: Nothing wrong :). Thanks.
KennyTM