Solved

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

Posted on 2011-09-06
9
258 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:knowlton
  • 6
  • 3
9 Comments
 
LVL 82

Expert Comment

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

Author Comment

by:knowlton
ID: 36492081
Wow...I was close!
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 5

Author Comment

by: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 82

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: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: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: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:knowlton
ID: 36492242
thx
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

831 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