/    Sign up×
Community /Pin to ProfileBookmark

Pass id value to javascript

I have this piece of code:

<a id=”idname” onclick=”getValue(this)”>link</a>

I have this javascript code:

function getValue(idval){
alert(getElementById(idval));
}

Obviously, this doesn’t work. If someone could tell me how to alert the ID value of the link that is clicked, that would be greatly appreciated.

Thanks,
Klanga

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@billyjacobsDec 06.2007 — When you do the following:

<a id="idname" onclick="getValue(this)">link</a>

the control itself is "this". So in your method you already have the control and there is no need to get it by calling getElementById. So this is what you should have:

function getValue(control)

{

alert(control.id);

}

// This is passing in the hyperlink control.

<a id="idname" onclick="getValue(this)">link</a>


If you just need the id you could write the function to take a string and then call it like

function getValue(idval)

{

alert(idval);

}

//Here you are passing in the id which you know from the element

<a id="idname" onclick="getValue('idname')">link</a>
×

Success!

Help @klanga2049 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.6,
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,
)...