Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generate id attribute of <p> tag dynamically

 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to generate id attribute of

tag dynamically by appending a javascript variable.

i tried the below code. but it does not work.



please help me. thank you.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64975
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on JSP. For more information, please read this.

This post has been moved to a more appropriate forum.
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This won't work because the javascript executes on your browser . You need to create the ids for your paragraph tags on the server.(meaning this should be done in java)

I'm not sure what this custom tag does , but please take a look at the custom Tag documentation they might have provided a counter for this purpose.

Thanks,
Srikkanth
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64975
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can certainly be done in JavaScript on the client as part of page initialization, but I'd do it on the server with JSP. Why wait to do it on the client?
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup I agree that it can be done on the client side, what i meant was the code which was posted won't work. Thanks for clearing the ambiguity.
By the way you can JSTL core tag forEach for this purpose (I suppose that you are trying to use struts iterator)

Thanks,
Srikkanth
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srikkanth Mohanasundaram wrote:
I'm not sure what this



this is struts2 iterator tag for looping.

but my query is related to javascript only. in the statement,

i want to assign id as para_1, para_2 etc. in the loop. ctr value is incrementing in the loop.

there must be a way of doing it at client side. if anyone knows then please give me a hint.

 
Paul Clapham
Sheriff
Posts: 21139
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vikas sharmaa wrote:there must be a way of doing it at client side. if anyone knows then please give me a hint.


Here's a hint: if you want to write a loop over some list on the client side, then that list has to exist at the client. This list doesn't. So if you insist on not doing it the obvious and natural way, you'll have to figure out some way of getting the list to the client. I don't particularly want to help you do that because I don't think it's a good idea.
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paul,

i am getting the list from the server in the form of mylist.

let me elaborate the problem here:

i have an iterator loop that contains the bunch of fields. these fields are categorised into < p > tags to use in DOM to hide them on selecting a checkbox.

now, i want id attribute of < p > tag to be different in each iteration. for that, i am appending ctr var to the id tag. but it is not working. please help.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Bear has already stated, JavaScript has nothing to do with your iterator. The incrementing ids should be calculated with your Java code.

Eric
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm actually going to move this to the Struts forum, as it's Struts-specific as described.

This has nothing to do with JavaScript--why do you want to use JavaScript to do something that's already provided for you?http://struts.apache.org/2.1.8/docs/iterator.html
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
awesome newton!!! thank you very much for making things simple.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic