• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Pass value from a function into an onclick

Here's what I have.
 
1st anchor tag that launches initiates the transfer: <a href="" onclick"functionOne('uniqueInfo');">Click Me</a>
 
2nd anchor tag that should receive the info: <a href="" id="uniqueId" name="uniqueId" onclick="">Click Me Next</a>
 
The JS I have basically does this:
 
functionOne(info) {
  myVariable = document.getElementById('uniqueId').onclick.value;
  myVariable = 'functionTwo(\'' + info + '\');';
}
 
functionTwo(moreInfo) {
    ...
}
 
So, what it's supposed to do is take the data from the first anchor and pass it into functionOne.  functionOne should set the onclick value of anchor 2 to be functionTwo('uniqueInfo');
 
I've set an alert after the second line in functionOne (ie, alert(myVariable); ) and it alerts the correct info.  But that info ISN'T being inserted into the onclick attribute of anchor 2 at all.
0
crazycharlie
Asked:
crazycharlie
  • 3
  • 3
  • 2
1 Solution
 
thirdCommented:
pass it through a global variable. like,

var myVar = '';

functionOne(info) {
  myVar = info;
}
 
functionTwo() {
    alert(myVar);
}
0
 
Michel PlungjanIT ExpertCommented:
and make sure you do

 
<a href="#" onclick="functionOne('uniqueInfo'); return false">Click Me</a>
0
 
crazycharlieAuthor Commented:
Thanks, third.  I actually figured it out and was coming back here to disable my question.  No biggie... :D

mplungjan, I actually used href="javascript:void(0);"

Cr@zyCh@rlie
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Michel PlungjanIT ExpertCommented:
I would STILL use

<a href="#" onclick="functionOne('uniqueInfo'); return false">Click Me</a>

since that does not give a 404 if you disable javascript
0
 
crazycharlieAuthor Commented:
Thanks, mplungjan.  I didn't think about the non-javascript aspect.
0
 
thirdCommented:
i agree with michel.

onclick"

should be

onclick="
0
 
Michel PlungjanIT ExpertCommented:
thanks, Third...
0
 
crazycharlieAuthor Commented:
Thanks, guys.  I had the '=' in there... was just a typo when I entered the original code.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now