• Post Reply Bookmark Topic Watch Topic
  • New Topic

I want to pass 2 element ids to a single event handler function onclick. is it possible?  RSS feed

 
Gowthami Bethanabotla
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 input elements of type text and hidden


the hidden element's id also should be dynamic as there are many hidden elements in my code.

So, on click of text type element, hidden element's value also should be set. Is it possible? if yes, can anyone please reply with appropriate code?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65964
140
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With enough code in the onclick attribute you could probably do it -- but that's going to be really clunky and fragile. Why are you needing to of this? We can likely come up with a better way.

Are you not using jQuery? If not, why not?
 
Dave Tolls
Ranch Hand
Posts: 2652
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:With enough code in the o'clock attribute you could probably do it


Auto-correct playing up?
:)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65964
140
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. Sigh -- fixed. Thanks.
 
Gowthami Bethanabotla
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear Bibeault

Yeah, JQuery also can be used. but Im not much comfortable with it.
If you suggest that Jquery would be better for this type of requirement, I will go for that.
Can you please specify how I can call a Jquery function with 2 element id's dynamically and change their values in JQuery function  ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65964
140
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jQuery would make this much easier.

You wouldn't need id attributes at all. The clicked element is available in the event handler as this, and then you can find the sibling element without needing an id.

The jQuery methods to focus upon are click() to establish the click handler, closest() to find parent elements, and find() to find child elements.

Give it a try and see how close you can get.
 
Gowthami Bethanabotla
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault

Thanks for suggestion!!

I have used siblings() in the following way. Please have a look


On click of text type element, hidden element's value also should be set. So, I am trying to use the below jQuery script


with "$(this).siblings('input:hidden').val(i); " line , variable "s" value is undefined. nothing is happening
I am not able to get the existing hidden element value to a variable by "var x= $(this).siblings('input:hidden').val();"

I have tried using find() as well by

var s=$(this).find('input[type=hidden]').val(i);  - even though no luck..

Can you please suggest me where I did wrong ?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65964
140
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two observations:

  • Here's the docs for the :hidden pseudo-selector: https://api.jquery.com/hidden-selector/ Is it really what you need?


  • The .find() method searches the children of an element. Is it what you need?
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!