i am using HTMLUNit to set the value of a form hidden element as below.
HtmlHiddenInput hidden = (HtmlHiddenInput) page.get("someid");
hidden.setValueAttribute(seriesName);
But this does not work as expected and throw an castException.
I have div element like follows
<div class="myclass">
<form:hidden id="someid" htmlEscape="true"/>
</div>
How can i set the value for that hidden form id using HTMLUNIT.
Thanks.
You don't need to use HtmlHiddenInput. If you are getting a cast exception, use a base object, as DomElement.
DomElement myInput = page.getElementById("someid");
myInput.setAttribute("value", seriesName);
So with any html dom element, the value attribute will be set to seriesName
Related
I want to get the value of the custom attribute by jquery.
I use this code to do that but in console it show "undefined":
var correct = $("div").data("correct");
this is the html:
<div data-correct="#classCorrect.Answer">....</div>
also, I used "attr" to do that but that was the same too and shows "undefined".
And I don't want to use "id" for the selector.
I want it to show 3 as it is in "#classCorrect.Answer".
use .data() if you use newer jQuery >= 1.4.3 and the part after data- must be lowercase, e.g. data-idNum will not work, but data-idnum will.
Use attr:
var correct = $("div").attr('data-correct');
I am sure that you have several divs in your html so you have to assign id to your div at first:
<div id="correctAnswer" data-correct="#classCorrect.Answer">....</div>
after this you can get data attribute value using .attr:
var correct = $('#correctAnswer').attr('data-correct');
or if you use newer jQuery >= 1.4.3 you can try .data
var correct = $('#correctAnswer').data('correct');
If there are serverl divs with the data-correct attribute, you should give them the same class or name
Such as:
<div class="test" data-correct="1">A</div>
<div class="test" data-correct="2">B</div>
<div class="test" data-correct="3">C</div>
And when you use the class or name selector, the result is an array of these elements, you should traverse it to get each element then get its attribute value.
$(function () {
var datalist = $(".test");
$.each(datalist, function (i,value) {
console.log($(value).data('correct'));
})
})
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 trying to implement client side validation in Struts 2. my theme is xhtml. The javascript generated is not able to validate my code. After debugging , I found that Struts is using the following notation to refer the elements.
form = document.getElementById(<form id>);
service = form.elements['service'];
the point is that service is coming as undefined.
when I checked that form.elements is null; However if I access form using document.formname i am able to see the fields in elements collection.
I am thinking document.forms[0] is returning the same object as document.getElementById(formid). What is the difference?
The form element can access fields by name, for this purpose you should get the form element. You can do it in many ways, use document.getElementById() or document.forms[], or $("#formid"). Whatever way you choose doesn't matter. Just note that a document can contain many forms, so you should reference a correct one. Getting form element by id returns an element that has an id attribute, getting it by the index in the forms property you should know the correct index. Once you get the form element you can reference input fields by name. For example
<form id="formid">
<input name="service">
</form>
<script>
var v = document.getElementById("formid")['service'];
</script>
So I have the following html source:
<form action='http://example.com' method='get'>
<P>Some example text here.</P>
<input type='text' class='is-input' id='agent_name' name='deviceName' placeholder='Device Name'>
<input type='hidden' name='p' value='firefox'>
<input type='hidden' name='email' value='example#example.com'>
<input type='hidden' name='k' value='cITBk236gyd56oiY0fhk6lpuo9nt61Va'>
<p><input type='submit' class='btn-blue' style='margin-top:15px;' value='Install'></p>
</form>
Unfortunately this html source is saved as a string.
I would like to parse it using something like jsoup. and obtain the following String:
<input type='hidden' name='k' value='cITBk236gyd56oiY0fhk6lpuo9nt61Va'>
or better yet, only grab the following value: cITBk236gyd56oiY0fhk6lpuo9nt61Va
The problem I'm running into is that:
a) that value: cITBk236gyd56oiY0fhk6lpuo9nt61Va is consistently changing I cannot look for the entire html tag.
So, I am looking for a better way to do this.
Here is what I currently have that does not seem to be working:
//tried use thing, but java was angry for some reason
Jsoup.parse(myString);
// so I used this instead.
org.jsoup.nodes.Document doc = Jsoup.parse(myString);
// in this case I just tried to select the entire tag. Elements
elements = doc.select("<input name=\"k\"
value=\"cITBkdxJTFd56oiY0fhk6lUu8Owt61Va\" type=\"hidden\">");
//yeah this does not seem to work. I assume it's not a string anymorebut a document. Not sure if it
//would attempt to print anyway.
System.out.println(elements);
so I guess I can't use select, but even if this would work. I was not sure how to place select that part of the tag and place it into a new string.
You can try this way
Document doc = Jsoup.parse(myString);
Elements elements = doc.select("input[name=k]");
System.out.println(elements.attr("value"));
output:
cITBk236gyd56oiY0fhk6lpuo9nt61Va
Try this call to select to get the elements:
elements = doc.select("input[name=k][value=cITBkdxJTFd56oiY0fhk6lUu8Owt61Va]")
In this context, elements must be an Elements object. If you need to extract data from elements, you can use one of these (among others, obviously):
elements.html(); // HTML of all elements
elements.text(); // Text contents of all elements
elements.get(i).html(); // HTML of the i-th element
elements.get(i).text(); // Text contents of the i-th element
elements.get(i).attr("value"); // The contents of the "value" attribute of the i-th element
To iterate over elements, you can use any of these:
for(Element element : elements)
element.html(); // Or whatever you want
for(int i=0;i<elements.size();i++)
elements.get(i).html(); // Or whatever you want
Jsoup is an excellent library. The select method uses (lightly) modified CSS selectors for document queries. You can check the valid syntax for the method in the Jsoup javadocs.
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.