4 Replies - 19418 Views - Last Post: 01 September 2009 - 01:33 AM

#1 W3bDev   User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Replace all text in a class

Posted 30 August 2009 - 03:29 PM

Hello all,

I dont understand JQuery much, so I figured I'd post what should be a pretty beginner question here... How do i replace all text within a particular class on a page. Say I have a div with class of "phoneNumber" that displays phone numbers throughout a page, then I want to replace each text with some new text, what's the simplest way of doing such a thing? I tried something like this, but it didn't work...

$('.phoneNumber').append('MyNewText');



Is This A Good Question/Topic? 0
  • +

Replies To: Replace all text in a class

#2 moopet   User is offline

  • binary decision maker
  • member icon

Reputation: 345
  • View blog
  • Posts: 1,190
  • Joined: 02-April 09

Re: Replace all text in a class

Posted 30 August 2009 - 04:12 PM

View PostW3bDev, on 30 Aug, 2009 - 09:29 PM, said:

Hello all,

I dont understand JQuery much, so I figured I'd post what should be a pretty beginner question here... How do i replace all text within a particular class on a page. Say I have a div with class of "phoneNumber" that displays phone numbers throughout a page, then I want to replace each text with some new text, what's the simplest way of doing such a thing? I tried something like this, but it didn't work...

$('.phoneNumber').append('MyNewText');



Nearly. Since you're doing it to a bunch of elements identified by class, you probably want to use each() to apply your change to each element in succession. This allows you to change that text in context.

$(".phoneNumber").each( function() {
	$(this).text('MyNewText')
});


Was This Post Helpful? 0
  • +
  • -

#3 Wimpy   User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Replace all text in a class

Posted 31 August 2009 - 06:48 AM

View Postmoopet, on 31 Aug, 2009 - 01:12 AM, said:

Nearly. Since you're doing it to a bunch of elements identified by class, you probably want to use each() to apply your change to each element in succession. This allows you to change that text in context.


You don't even need to .each() it, according to the documentation ( "Set the text contents of all matched elements." ):
$(".phoneNumber").text('MyNewText');


Hope it helps! :)

This post has been edited by Wimpy: 31 August 2009 - 06:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 moopet   User is offline

  • binary decision maker
  • member icon

Reputation: 345
  • View blog
  • Posts: 1,190
  • Joined: 02-April 09

Re: Replace all text in a class

Posted 31 August 2009 - 12:23 PM

View PostWimpy, on 31 Aug, 2009 - 12:48 PM, said:

View Postmoopet, on 31 Aug, 2009 - 01:12 AM, said:

Nearly. Since you're doing it to a bunch of elements identified by class, you probably want to use each() to apply your change to each element in succession. This allows you to change that text in context.


You don't even need to .each() it, according to the documentation ( "Set the text contents of all matched elements." ):
$(".phoneNumber").text('MyNewText');


Hope it helps! :)


Sorry, yes, that's correct. What I was implying was that giving the function the context of the element means you have full control of what goes on, so you can use $(this) in your function. But you're right, and it's simpler to explain your way.
Was This Post Helpful? 0
  • +
  • -

#5 Wimpy   User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Replace all text in a class

Posted 01 September 2009 - 01:33 AM

And it is really a good thing of you to point out that the .each()-function exists and how it works, but when I read your last post it sounded as if you were certain that you had to use the .each()-function to do it! Just wanted to clear that out! :) And yes, by using the .each()-function you get a lot more control over what goes on and you can also do a lot more advanced stuff with the matched elements that way (animation, validity checking, etc). :)

View Postmoopet, on 31 Aug, 2009 - 09:23 PM, said:

Sorry, yes, that's correct. What I was implying was that giving the function the context of the element means you have full control of what goes on, so you can use $(this) in your function. But you're right, and it's simpler to explain your way.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1