/    Sign up×
Community /Pin to ProfileBookmark

Passing variable into event listener

I’m changing my coding style by using event listener instead of “onclick=()” pattern I’ve used for many years, and so far so good. Now I have a problem looking for a way to easily pass some values into the function.

Let say I’m rewriting my webboard. There are “Edit / Delete post” here and there for Moderators and High ranking users. The edit form will be loaded and display in the reply content area by AJAX. Normally, I’d use

[code=html]
<a href=”#” onclick=”getEditReply(1003)”>Edit</a>
<p id=”reply_1003″>Some content here</p>
[/code]

With Jquery, I changed it to

[code=html]
// at the page header
<script>
function getEditReply(id){
// some code to load the reply data and form, and put it inside the <p> of that reply
}
</script>

// the edit link
<a href=”reply/edit/1003″ class=”editReply” id=”editReply1003″>Edit</a>
<p id=”reply_1003″>Some content here</p>
<script>
$(“#editReply1003”).click(function(){getEditReply(1003);});
</script>
[/code]

The current method works well, but I think there might be easier and better way than that…
How can I make the event listener to pass the ID value into the function automatically? I want to put script like this at the header instead but I don’t know what to do with ID,

[code=html]
<script>
$(“a.editReply”).click(function(){
getEditReply(The_ID_of_the_reply);
});
</script>
[/code]

Any suggestion will be appreciated ?.

PS. I’m thinking about putting a hidden input with ID value in each reply block, and let jquery look for it by using .siblings() or .next()…

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyNov 03.2008 — Have you tried just using the [b]this.id[/b] reference in your event handler?
&lt;script&gt;
$("a.editReply").click(function(){
getEditReply(this.id);
});
&lt;/script&gt;
Copy linkTweet thisAlerts:
@aquaryauthorNov 03.2008 — in that case, the function would get "editReply1003" instead. I want only "1003" ?.

The flow of the script should be,

- Function get the ID of the reply (not the ID of any elements).

- the id is sent to Ajax and PHP script to get the content of that reply.

- the edit form is returned and replace the reply content

using this.id would work if I used id="1003", but id must begin with a-z/A-Z...
Copy linkTweet thisAlerts:
@MrNobodyNov 03.2008 — As part of your code, just separate the 1003 from the id of the link.
Copy linkTweet thisAlerts:
@aquaryauthorNov 03.2008 — Aren't there any other way around? although it's not hard to do so, I just hope for the shortest/best way possible. ?
Copy linkTweet thisAlerts:
@MrNobodyNov 03.2008 — The only "automatic" way to pass data to an event handler is via some property of the object to which the event handler is attached. Otherwise, you have to hard-code the data somewhere -- in the object, in the handler, or in JavaScript variables (usually, arrays).
Copy linkTweet thisAlerts:
@aquaryauthorNov 03.2008 — I see. Thank you for your answer ?
Copy linkTweet thisAlerts:
@MrNobodyNov 03.2008 — Of course, there are also ways to get a pointer to another object using some relationship through the DOM. For example, if both the link and the paragraph are inside of some other container, you could use something like the following to get a pointer to the paragraph from inside the event handler which is attached to the link:
[CODE]var obj = this.parent.getElementsByTagName("P")[0];[/CODE]
Copy linkTweet thisAlerts:
@rnd_meNov 03.2008 — 
[CODE]var obj = this.parent.getElementsByTagName("P")[0];[/CODE][/QUOTE]


this.parent is non-standard.

use this.parentNode
Copy linkTweet thisAlerts:
@MrNobodyNov 03.2008 — Sorry for the memory hiccup. I normally do use parentNode. ?
Copy linkTweet thisAlerts:
@Declan1991Nov 03.2008 — Don't forget that you can match the number out of the id.this.id.match(/d+$/)Or some code similar to that.
×

Success!

Help @aquary spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...