I need to get the value of the styleClass element in javascript. Page is in jsp with struts/html tag elements. jsp code is
<input type="hidden" class="filename" name="filename" value="<%= filename %>" />
<html:file property="testfile" styleClass="testfile"/>
and onclick of button I invoke the javascript
function fields() {
var filename = jQuery('.filename');
alert(filename);
var testfile= jQuery('.testfile').val();
alert(testfile);
}
However in the firstcase(filename) I get [object object] returned and in second case I get "undefined". Can someone give some pointers on how to get the uploaded file name in jquery. Thanks
If you want to get a value from an input:
var filename = $('.filename').val();
or
var filename = jQuery('.filename').val(); (same as above)
read more here -> jquery selector can't read from hidden field
Try this
var testfile= jQuery('.testfile').attr('value');
It may be that your browser is preventing access to the value attribute for security reasons. Try both options in different browsers - Chrome, Firefox, IE, etc.
Related
I'm trying to create invisible divs with jQuery which in title will be populated dinamically by an struts tag, but its giving me invalid indexed property error:
Invalid indexed property 'niveisRisco['+<%=ctr%>+']
Heres the code i'm using:
$(function(){
var mapaAplicacaoJs = $('#MapaAplicacao area');
var mapaPerfilJs = $('#MapaPerfil area');
<logic:present name="carteiraSelecionada">
<logic:iterate name="carteiraSelecionada" property="carteiraAtual.niveisRisco" id="foo" indexId="ctr">
mapaAplicacaoJs.each(function() {
mapaAplicacaoJs.before('<div id="nivel_risco_dv" class="tabindex" title="<bean:write name="carteiraSelecionada" property="carteiraAtual.niveisRisco['+<%=ctr%>+'].getDescricao" />"></div>');
</logic:iterate>
</logic:present>
});
If i hardcode <div id="nivel_risco_dv" class="tabindex" title="<bean:write name="carteiraSelecionada" property="carteiraAtual.niveisRisco[0].getDescricao it works.
Can anyone tell me whats wrong with my concatenation?
Thanks
There is not attribute with name indexId in logic:iterate tag. See logic:iterate
Access it with $ctr instead of doing it via scriplet
I am building an Android app and I want to post a html form that looks like this:
<form onsubmit="ShoutBox.postShout(); $('shout_data').value = ''; return false;">
Shout: <input type="text" id="shout_data" size="50"> -
<input type="submit" value="Shout Now!" id="shouting-status"></form>
I am using Jsoup in the rest of the application and I would preferably use it for this aswell.
I know about the .data(name, value) method, but since the html text and button don't have name attributes, that's not working. I can extract the field by using the ids and fill the field with: Element.val(String val); But I don't know how to post the form after that. Can anyone help me?
This is the JavaScript code for posting it:
postShout: function() {
message = $("shout_data").value;
if (message == "") {
return false;
}
$("shouting-status").value = ShoutBox.lang[0];
postData = "shout_data="+encodeURIComponent(message).replace(/\+/g, "%2B");
new Ajax.Request('xmlhttp.php?action=add_shout', {method: 'post', postBody: postData, onComplete: function(request) { ShoutBox.postedShout(request); }});
},
The post is not done via a form submit and post variables but via JavaScript and an XML HTTP request. JSoup is incapable to execute JavaScript. You need a browser that you can remote control. To do this headless in Java HTMLUnit is a good choice.
I need to post a text-field value to server but i have not placed the text-field in side the form tag.Here is the details of my use-case
i have an anchor tag like
LOGIN
This anchor tag is not inside any form tag and i need to send one extra value to the server and don't want that value to append as query string.
I have created a hidden filed and have provided the required value to that hidden field, but when i click on the Login link and its getting to my Controller class this hidden field value is not available.
Is there any way to send that value to server side class as a request parameter?
You can use ajax to do that, i suggest to use Jquery
$.post('loginhandle', {username:$('#username').val(), password: $('#password').val()} function(){});
By using Javascript get value from hidden fields like this
<script>
var name= document.getElementById("login").value
document.getElementById("topage").innerHTML='LOGIN'
</script>
<input type="hidden" name="name" value="ashraf" id='login'>
<div id='topage'>
LOGIN
</div>
You're doing a get rather than a post. You could append to he querystring as this works with get.
Get the hidden field value using javascript before the form is submitted. Use
document.getElementById("hiddenID").value; Append the value obtained in the URL before the form is submitted. The value should be there in the server.
Regards,
Ajai G
I have a struts2 action with a field: private Long omradeId;
The field has a getter.
The action is sent to a jsp and within that jsp i can access the field using <s:property>tag. Thats all good.
Now i also have within the jsp a section where i define a <script>. Within that script i would like to create a variable that will build a url with the above mentioned struts2 field as a value.
<script type="text/javascript">
var url = "/path/to/action?parameter1=";
</script>
How can i put the value of omradeId after the equals (=) sign? I tried using <s:property>but that did not work.
Any suggestions?
"/path/to" will change depending on the web server. To avoid this use the struts2 url tag.
See: http://struts.apache.org/2.x/docs/url.html
For an action called "action" in namespace "/" with a parameter called parameter1 having the value omradeId, you would simply say:
<s:url namespace="/" action="action">
<param name="parameter1" value="%{omradeId}"/>
</s:url>
putting the above into the JS variable we have:
var url = "<s:url action="action"><param name="parameter1" value="%{omradeId}"/></s:url>";
Using the above will mean your application can be installed on different application servers without change.
Having formated xml is nicer than inline, if using a lot of parameters adding the var parameter to the s:url tag to give it a name and then you can reference this string in a number of places with the s:property tag would keep things clean.
<s:url namespace="/" action="action" var="myString">
<param name="parameter1" value="%{omradeId}"/>
</s:url>
var url = "<s:property value="#myString"/>";
This should work:
<script type="text/javascript">
var url = "/path/to/action?parameter1=<s:property value="omradeId">";
</script>
If not you should check if the value is not null and value is successfully set in your action class.
I am working on a Struts2 application. I am setting the value of a hidden field in JSP with the purpose to access it by JavaScript.
My JSP code:
<s:iterator value="collegelist">
<tr>
<td align="center"><s:property value="collegename"/></td>
<s:hidden name="hiddenname" key="collegename" />
</tr>
</s:iterator>
My JS code:
var myForm = document.frmAction;
var text = myForm.hiddenname.value;
alert("hidden field text is:" + text);
The alerts shows a blank value.
What is the cause and how can I solve this?
Try
element = document.getElementsByName("hiddenname");
alert(element[0].value);
You generate multiple fields having the same name, since your code is inside a s:iterator tag. You should obviously have such a loop in your Javascript as well :
var hiddenFields = document.getElementsByName("hiddenname");
for (var i = 0; i < hiddenFields.length; i++) {
alert("hidden field text is::" + hiddenFields[i].value);
}
Also, verify the the value is not blank in the generated HTML, and that the hidden fields'a name is hiddenname.
I tried your code and it surely works.. problem is somewhere in your server code itself..
Look here: http://jsbin.com/ajajo4/2/edit
Make sure you have only one form with the name "frmAction" and only one hidden field with the name "hiddenname". If you have multiple, you'll get an array instead of a single value.
The root of the problem is that you are inside of an iterator. Struts updates the name for you in order to correctly hook everything up. If you pull up your page and view source, your hidden field will probably look something like this:
<input type="hidden" name="collegelist[0].hiddenname" value="thename"/>
Regardless, if you want the retrieval by name to work, do not trust the name that you supply to a struts tag. Always pull up the generated source and look at what name the field actually has.