View Full Version : Need to get the value from links
shandutta
11-18-2002, 09:49 AM
Hi
I need to get the value from the link when clicked.
I have a list of projects which are links. On click I need to get the value which project is clicked.
Any help is highly appreciated.
Thanks
Shantanu
shandutta@yahoo.com
asp-hosting.ca
11-18-2002, 11:35 AM
Use onclick event:
<a href="#" onclick="alert('Link1');">Link1</a><br>
<a href="#" onclick="alert('Link2');">Link2</a><br>
<a href="#" onclick="alert('Link3');">Link3</a>
shandutta
11-18-2002, 11:23 PM
Thanks.
But in my case the links are generated from a vector containing the list of projects. So while using onclick event, I get the the last value from the list even if I click other project links.
My code is as follows :
#######################################################
<%
Enumeration enum = storedTable.elements();
while( enum.hasMoreElements() ) {
dContent = ( Projects ) enum.nextElement();
String pName = dContent.projectName.toLowerCase();
projectName = pName;
%>
<tr >
<td nowrap=1 >
<div class="">
<A class="" HREF="/augustus/list_curr_release.j
sp" onclick="projectName = pName"><%= dContent.projectName %> </A><br>
</div>
</td>
</tr>
<%
}
%>
#######################################################
In this case, projectName = pName always gives me the last value of the vector which is different from what is displayed through " <%= dContent.projectName %> " while viewing the page.
Thanks in advance.
Shantanu
shandutta@yahoo.com
kdjoergensen
11-19-2002, 01:24 PM
The onclick eventhandler excecutes scripts as a method of the object it is located inside. E.g. when an onclick event handler is located inside a link it takes the link as it's realm.
There is a keyword in javascript called 'this' and it will contain an object reference to the object which contain it when the javascript is called as a method of this object (which the eventhandler will do).
From the object reference you can extract the values of any of the attributes defined.
example:
<a href="http://www.disney.com/" onclick="alert(this.href)">Mickey</a>
Another example:
<a href="http://www.disney.com/" name="walt" onclick="alert('Visit '+this.name+' at: '+this.href)">Mickey</a>
Hope this was clear..
shandutta
11-20-2002, 02:00 AM
Thanks. I got some pointers.
But "this.name" is returning "undefiend" even though I set name="Shantanu".
Besides, have another problem.
I want to compare a javascript variable with a JSP variable. The thing is the JSP variables are within a vector and I want to loop through the vector and compare each element with the javascript variable. This I am doing within a function "setAttribute" which I call on "onclick" event.
But I am not able to loop through the vector, and the counter is always =0, and points to the first element of the vector.
The code is as follows :
######################################################
<script type="text/javascript">
function setAttribute1(txt) {
alert( txt )
<%
projListLength=0;
projNameListRemovable = projNameList ;
Vector vecElems;
Enumeration enumPVal = projNameList.elements();
%>
alert( '<%= projNameListRemovable.size() %>' )
alert( '<%= projNameListRemovable %>' )
for( var f=0; f < <%= projNameList.size() %> ; f++ ) {
<%
vecValue = (String)projNameListRemovable.get(projListLength);
// counter not increasing
// vecValue = ( String )enumPVal.nextElement();
// nextElement() always gives the first element
%>
alert( '<%= vecValue %>' )
alert( '<%= projNameListRemovable %>' )
if( txt.toLowerCase() == '<%= vecValue %>' ) {
alert( ' values are same ' )
<%
ssion.setAttribute("projectName", vecValue );
// projNameListRemovable.removeElementAt(projListLength);
increment(); // calling this function to increment the counter "projListLength", but to no avail.
%>
break
}
else{
alert( ' value not equal ' )
<%
increment();
// projListLength++;
// projNameListRemovable.removeElementAt(projListLength);
%>
// continue
}
}
}
######################################################
Any workaround or pointer will be highly appreciated.
Thanks
Shantanu.
shandutta@yahoo.com
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.