Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

select all divs with id containing A and set their inner text to the full id

Posted on 2011-09-06
9
Medium Priority
?
265 Views
Last Modified: 2012-05-12
<div id='landmarkY' class='landmarkY'></div><!-- end of div landmarkY -->


What would be the jquery to set the innerText to the same thing as the id, like this:


<div id='landmarkY' class='landmarkY'>landmarkY</div><!-- end of div landmarkY -->

There are over 20 of these, so I need a generic way to refer to them, like "gather all divs with id containing "landmark" and set their inner text to the full id of said tag"

   $('div[id*="landmark"]').each(function ()
            {
                $(this).text(  ???????? not sure what
            });
0
Comment
Question by:Tom Knowlton
  • 6
  • 3
9 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 36492072
$('div[id*="landmark"]').each(function ()
            {
                $(this).text( $(this).attr("id") );
            });
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36492081
Wow...I was close!
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36492092
test page : http://jsfiddle.net/ef75z/

using a unique class :

$('div.landmark').each(function ()
            {
                $(this).text( $(this).attr("id") );
            });

new test page : http://jsfiddle.net/ef75z/1/
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36492153
$('div.landmark').each(function ()
            {
                $(this).text( $(this).attr("id") );
            });


what is 'div.landmark' selecting?

is it the same same thing as this:

$('div[id*="landmark"]')
0
 
LVL 83

Expert Comment

by:leakim971
ID: 36492170
all the div with a class landmark

>is it the same same thing as this:
>$('div[id*="landmark"]')

no (id contains)
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36492188
It would be cool if EE could embed jsfiddle.net into the jQuery zone...so we could have a handy little sandbox.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36492198
In the actual page, each div will have a unique class that it uses...so the first code was better for my purposes, I think.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36492239
>>>no (id contains)


I get it now.  I didn't realize the class name was the same each time.

For my purposes, I am ignoring class and using id.


Now, is there a way to cut out the "landmark" part and just display the characters after?

instead of:

landmarkA
landmarkZ
landmarkWhatever
landmarkFoo
landmarkBar

the inner text for the div would just be:

A
Z
Whatever
Foo
Bar
0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 36492242
thx
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question