Click to See Complete Forum and Search --> : referencing a function's caller


angrytuna
06-12-2003, 12:05 PM
I've done this at one point, but I can't remember how. If I have a tag:

<a href='javascript:foo();' value='???'></a>

and a function:

function foo(){
//obtain value ??? from caller
}

how do I get ???:confused:
keyword "this" returns [Object Window].

Any help appreciated.

AT

scriptkid
06-12-2003, 12:22 PM
this.value

you should pass a parameter to the function tho cause i dont think the anchor tag has a value attribute...of course ie dosent really care what attributes it has cause u can make ur own but still...

function foo( value )
{
document.write(value);
}

<a href="javascript:foo('Hello');">Hello</a>

Jona
06-12-2003, 12:27 PM
<a href="default.html" onClick="foo(this);">Text</a>

<script type="text/javascript">
<!--
function foo(anchor){
alert(anchor.href); // alerts, "default.html"
}
//-->
</script>


Jona

scriptkid
06-12-2003, 12:31 PM
y pass the whole object when all she wanted was one attribute tho...just go ahead and do this.href or this.value whichever attribute ur trying to access no need for the whole object unless u plan on doing mroe with it

Charles
06-12-2003, 12:40 PM
<script type="text/javascript">
<!--
function foo(e) {alert (e.id)}
// -->
</script>

<a href="#" onclick="foo(this)" id="someFoo">foo</a>

<hr>

<script type="text/javascript">
<!--
function fum(e) {alert (e.value)}
// -->
</script>

<a href="#" onclick="this.value = 'someFum'; fum(this)">fum</a>

<hr>

<script type="text/javascript">
<!--
onload = function () {for (j=0; j<document.links.length; j++) {document.links[j].fee = function () {alert (this.value)}}}
// -->
</script>

<a href="#" onclick="this.value='someFee'; fee()">fee</a>

Jona
06-12-2003, 12:40 PM
Originally posted by scriptkid
[U]nless [you] plan on doing m[or]e with it.

Exactly why I posted it. By getting the whole object, you can change and read its properties, rather than confusing "angrytuna" and making him end up with foo(this.value, this.href); instead of the simplified way of getting the whole object and refering to its properties. Also, if you use this.value and pass it to the function, and try to change "this.value" you will simply change a variable value. Consider the below:


<input type="button" value="Click Me" onClick="foo(this.value);">

<script type="text/javascript">
<!--
function foo(button_Value){
//This script should change the value of the button, onClick
button_Value = "New Button Text";
}
//-->
</script>


The above will not change the value of the button, because it has a variable passed to it; therefore the variable is receiving a new string and no longer contains the button's value as a string. Instead, we should refer to the entire object to edit its properties:


<input type="button" value="Click Me" onClick="foo(this);">

<script type="text/javascript">
<!--
function foo(buttonObj){
//The below script will edit the value of the above button
buttonObj.value = "New Button Text";
}
//-->
</script>


Jona

angrytuna
06-12-2003, 12:43 PM
is there a way to reference the tag as an object?

for instance:

<a href="default.html" onClick="foo(this);">Text</a>

Jona
06-12-2003, 12:45 PM
I think I just explained that in my above post. :D But tell me if you need more help/info.

Jona

Charles
06-12-2003, 12:48 PM
Originally posted by angrytuna
is there a way to reference the tag as an object?

for instance:

<a href="default.html" onClick="foo(this);">Text</a> Yes, and you have done so, as Jona and I have pointed out above. But, as I was trying to demonstrate, you can also give one or several of your links its own "foo" method that can reference its link with "this".

angrytuna
06-12-2003, 12:50 PM
Beg your pardon, incomplete post.

here is the page that I've got; prints the unicode characters.

<html>
<head>
<script language="javascript">
function getValue(some_link){
alert(some_link.value);
}
</script>
</head>
<body>
<script language="javascript">

for(i=1;i<=32;i++){
document.write("<tr>");
for(j=0;j<=32;j++){
k=i*j
document.write("<td>");
document.write("<a value='\&\#"+k+"' href='test.html' onClick='javascript:getValue(this);return false;'>&#"+k+"</a>");
document.write("</td>");
}
document.write("</tr>");
}
</script>
</body>
</html>

The getValue function is alerting bubkiss at the moment, and I think I've followed your examples all right (thank you, by the way). Have I missed something obvious?

AT

Jona
06-12-2003, 12:53 PM
The script works fine for me; although I'm not sure if there is something more you wanted.

Charles, where did you learn to script the way you do? I am interested in learning to script in this manner. Please give me some sort of reference. Thanks.

Jona

Charles
06-12-2003, 12:54 PM
Originally posted by angrytuna
Have I missed something obvious?Yes, you cannot go around making up your own attributes. It just isn't done in polite circles.

angrytuna
06-12-2003, 12:55 PM
That's strange. Mozilla reports the value as being undefined, while IE (on Mac) gives the correct result after about a 5 second delay...

I don't understand what you mean, Charles; which attributes am I making up?

Charles
06-12-2003, 12:56 PM
document.write("<a href='test.html' onclick='getValue(this);return false;'>�"+k+"</a>");
document.links[document.links.length-1].value = "someValue";

Jona
06-12-2003, 12:56 PM
That is because there is no real attribute, "value." I believe...

Jona

angrytuna
06-12-2003, 12:58 PM
Ah. Think I understand now. Thanks again for the help, both of you.


Cheers,
AT

Charles
06-12-2003, 01:01 PM
The HTML A element has no "value" attribute and parsers should just ignore one if try to define it. However, the JavaScript "link" object can be assigned any number of methods and properties but you have to do the assignment with JavaScript. I have demonstrated two ways to do that above.