Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javascript function not being called

 
Deeps Mistry
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using struts 1.1. My jsp is as follows:
<logic:iterate id="Challan" name="Challan">
<tr>
<td >
<bean:define name="Challan" property="products" id="products"></bean:define>
<html:text name = "Challan" property="perbox" size="5" onchange='<%= "javascript:calcBBSeries("+products+");"%>'></html:text>
</td>
</tr>

function calcBBSeries(value)
{
alert("Inside BBSeries");
alert(value);
}

I am not getting any alert. Also the generated code is as follows:
<tr>
<td >
<input type="text" name="perbox" size="5" value="0" onchange="javascript:calcBBSeries(Drawer);">
</td>
</tr>
<tr>
<td >
<input type="text" name="perbox" size="5" value="0" onchange="javascript:calcBBSeries(Units);">
</td>
</tr>

Where am i going wrong?

Please help me out.

 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you will discover what is going wrong if you open the page using the browser, and then take a look at the generated HTML source ;)
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It should work on seeing your generated source...Onchange event occurs after iteh textbox loses focus.. check it properly..
 
Deeps Mistry
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
omar al kababji wrote:you will discover what is going wrong if you open the page using the browser, and then take a look at the generated HTML source ;)


Hi,
This is the generated html source. I cant see anything wrong in this. It is getting the value: onchange="javascript:calcBBSeries(Drawer);", so why it isnt passing it to the function. I am not even getting the first alert. So the function is not being called. I fail to understand what is happening.

Please help me out.

<tr>
<td >
<input type="text" name="perbox" size="5" value="0" onchange="javascript:calcBBSeries(Drawer);">
</td>
</tr>
<tr>
<td >
<input type="text" name="perbox" size="5" value="0" onchange="javascript:calcBBSeries(Units);">
</td>
</tr>

 
Deeps Mistry
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balu Sadhasivam wrote:
It should work on seeing your generated source...Onchange event occurs after iteh textbox loses focus.. check it properly..


Hi,
I checked it. Its not working. If i replace it with <html:text name = "Challan" property="perbox" size="5" onchange="calcBBSeries('hi');"></html:text>, its working fine.
So the problem lies in 'javascript:calcBBSeries'.

What to do now?

Thanks.
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simple because you sould enclose the values you pass to the method with single quotations, in other words you should not pass the parameters as

Drawer .... but it should be... 'Drawer'
 
Deeps Mistry
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
omar al kababji wrote:simple because you sould enclose the values you pass to the method with single quotations, in other words you should not pass the parameters as

Drawer .... but it should be... 'Drawer'

Hi,
Yeah..you are right. I had tried that earlier and it was showing me this error:

org.apache.jasper.JasperException: /pages/viewchallan.jsp(226,88) Attribute value "javascript:calcBBSeries('"+products+"');" is quoted with ' which must be escaped when used within the value

So i had removed the quotes('').



 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could write it as follows:

onchange = 'javascript:calcBBSeries(${products});'
 
Deeps Mistry
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
omar al kababji wrote:you could write it as follows:

onchange = 'javascript:calcBBSeries(${products});'


not working...
this is what it generates:
<td ><input type="text" name="perbox" size="5" value="0" onchange="javascript:calcBBSeries(${products});"></td>

What else should i try?


 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reason why you output the bolded part inside that?

<%= "javascript:calcBBSeries("+products+");"%>

makes a lot more sense to only do <%= products %>

Also javascript: is a waste in event handlers, loose it.

Eric

 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not related to the actual problem, but using the "javascript:" pseudoprotocol makes no sense. The on* attributes always refer to Javascript by default.

Related to your actual problem: install and run a decent JS debugger. For example Firebug.

Edit: when posting this, the last message of Eric wasn't in view yet. So the first sentence is a bit superfluous.
Edit2: oh now I reread the problem a bit more closely, this isn't a problem with JS at all. Please move this topic to a JSP forum.
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what it generates:
<td ><input type="text" name="perbox" size="5" value="0" onchange="javascript:calcBBSeries(${products});"></td>


it seems that you have EL disabled, however by default it should be enabled, may be you should add the EL needed jars to your class path. by the way try to avoid the use of expressions of type <%= %> in your JSP's, they are old school.
 
Deeps Mistry
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
omar al kababji wrote:
this is what it generates:
<td ><input type="text" name="perbox" size="5" value="0" onchange="javascript:calcBBSeries(${products});"></td>


it seems that you have EL disabled, however by default it should be enabled, may be you should add the EL needed jars to your class path. by the way try to avoid the use of expressions of type <%= %> in your JSP's, they are old school.


hey, i have all the el related jars in my classpath. But still doesnt seem to work
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To use EL in template text you need at least a JSP 2.0 compliant container and the web.xml needs at least to be declared as Servlet 2.4 (which implies the use of JSP 2.0). Otherwise you'll need the JSTL's c:out tag instead. Do not use scriptlets in any way. There is in fact no valid business reason to use scriptlets.
 
Deeps Mistry
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bauke Scholtz wrote:To use EL in template text you need at least a JSP 2.0 compliant container and the web.xml needs at least to be declared as Servlet 2.4 (which implies the use of JSP 2.0). Otherwise you'll need the JSTL's c:out tag instead. Do not use scriptlets in any way. There is in fact no valid business reason to use scriptlets.


I tried using c:out tag as follows:

<html:text name = "Challan" property="perbox" size="5"onchange = 'javascript:calcBBSeries(<c:out value="abc" />)></html:text>

But its giving me an error saying attribute <c:out value="abc" /> has no value. Also i want to pass the value of product to the function. How do i do it?

Thanks'>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic