I am newbie to jquery. In my code I dynamically generate the radio button in jquery.when user clicked on the radio button I should get the siblings value.here is my jsp code.
<div id="result">
<c:forEach var="user" items="${model.userList}">
<table>
<thead>
<tr>
<td>Name</td>
<td>Is Approver</td>
</tr>
</thead>
<tbody>
<c:forEach var="disp" items="${user.value}">
<tr>
<td><c:out
value="${disp.getTblUserDetails().getUserName()}" /></td>
<td class="phase" style="display:none"><c:out value='${user.key.getId()}'/></td>
<td class="users" style="display:none"><c:out value='${disp.getTblUserDetails().getId()}'/></td>
<td><input type="radio" name="approver" class="some" /></td>
</tr>
</c:forEach>
</tbody>
</table>
</c:forEach>
</div>
My Jquery code:
$("#result").on("click",".some",function() {
event.preventDefault();
var phaseid= $(this).siblings('.phase').val();
var userid=$(this).siblings('.users').val();
alert(userid+" "+phaseid);
});
I am getting undefined in alert box.what's wrong with my code.how to I get siblings.
Any help will be greatly appreciated!!!!
.some is the input element inside a td so the .phase and .users are not siblings of this, they are the siblings of the parent of this. So
$("#result").on("click", ".some", function (event) {
event.preventDefault();
var $td = $(this).parent();
var phaseid = $td.siblings('.phase').text();
var userid = $td.siblings('.users').text();
alert(userid + " " + phaseid);
});
Also td does not have value, you may have to use .text()
Related
I need to capture the value of the submit button but pressing the button only sends me the value of the first generated button.
This is my code
modificarVivienda.jsp:
<table border="1">
<thead>
<tr>
<td>
Codigo vivienda
</td>
<td>
Direccion
</td>
<td>
Numero
</td>
<td>
Tipo vivienda
</td>
<td>
Condominio
</td>
<td>
Rut propietario
</td>
</tr>
</thead>
<tbody>
<c:forEach items="${lstviviendas}" var="v">
<tr>
<td>
${v.cod_vivienda}
</td>
<td>
${v.direccion}
</td>
<td>
${v.numero}
</td>
<td>
${v.tipo_vivienda}
</td>
<td>
${v.nombre_condominio}
</td>
<td>
${v.rut_propietario}
</td>
<input type="text" value="${v.cod_vivienda}" name="cod_vivienda" hidden="true">
<td>
<input type="submit" value="Modificar" id="btnModificar">
</td>
</tr>
</c:forEach>
</tbody>
</table>
Servlet:
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int cod_vivienda = Integer.parseInt(request.getParameter("cod_vivienda"));
DAOVivienda dv = new DAOVivienda();
HttpSession session = request.getSession();
ArrayList<Vivienda> vivienda = dv.buscarPorId(cod_vivienda);
session.setAttribute("viviendaAModificar", vivienda);
response.sendRedirect("vivienda/modificar.jsp");
}
"I need to capture the value of the submit button"
None of your submit buttons have a name, so no value is submitted for the button.
I think you meant to say that you want the value of the related <input> named cod_vivienda.
"pressing the button only sends me the value of the first generated button"
That is not correct. All the cod_vivienda values are submitted. You probably just called getParameter("cod_vivienda"), and javadoc says:
If you use this method with a multivalued parameter, the value returned is equal to the first value in the array returned by getParameterValues.
As you can see, to get all the values, you have to call getParameterValues("cod_vivienda").
To actually get a single parameter named cod_vivienda, with the value matching the button pressed, you should use the <button> element, which can associate a value with the button, separate from the displayed text.
Replace:
<input type="text" value="${v.cod_vivienda}" name="cod_vivienda" hidden="true">
<td>
<input type="submit" value="Modificar" id="btnModificar">
</td>
With:
<td>
<button type="submit" name="cod_vivienda" value="${v.cod_vivienda}">Modificar</button>
</td>
Now, only the name/value pair of the button pressed will be submitted to the server.
here i want to extract the href value from below code,
<table id="offers_table" class="fixed offers breakword" summary="" width="100%" cellspacing="0">
<tbody>
<tr>
<tr>
<tr>
<td class="offer onclick ">
<table class="fixed breakword ad_id1ezENl" summary="Ad" data-photos="2" width="100%" cellspacing="0">
<tbody>
<tr>
<td rowspan="2" width="164">
<div class="space">
<span class="rel inlblk detailcloudbox">
<a class="thumb vtop inlblk rel tdnone linkWithHash scale5 detailsLink"
href="https://www.olx.in/item/hyundai-accent-car-ID1ezENl.html#1a86c09693" title="">
</span>
</div>
</td>
<td valign="top">
<td class="wwnormal tright td-price" width="170" valign="top">
</tr>
<tr>
</tbody>
</table>
I have tried this below code but it shows the error
WebElement ele=driver.findElement(By.id("offers_table"));
WebElement href=ele.findElement(By.xpath("//tr[3]/span[#class='rel inlblk detailcloudbox']/a[#href]"));
System.out.println(href.getAttribute("href"));
You can use cssSelector insted of xpath for this case, try using below code:
String hrefvalue = driver.findElement(By.cssSelector("span.rel.inlblk.detailcloudbox > a")).getAttribute("href");
Try this xpath:
//a[#class='thumb vtop inlblk rel tdnone linkWithHash scale5 detailsLink']
[#href='https://www.olx.in/item/hyundai-accent-car-ID1ezENl.html#1a86c09693']
I have html code:
<table width="100%" cellpadding="5" cellspacing="2" class="zebra">
<tr>
<td colspan="5">
<div class="paginator">
2
</div>
</td>
</tr>
<tr>
<td>some_value</td>
</tr>
<tr>
<td>some_value</td>
</tr>
<tr>
<td colspan="2">
<div class="paginator">
2
</div>
</td>
</tr>
</table>
I use Jsoup. How can I get all links except links in div tag?
I try to do something like this, but It doesn't work. Element contains all the links.
org.jsoup.nodes.Elements tableText = doc.select("table.zebra").not("tr td div.paginator");
for (org.jsoup.nodes.Element td : tableText.select("td a")) {
System.out.println(td.attr("href")); // http://some_link
....
}
You can use the below code..
Document html = Jsoup.parse(htmlStr);
for (Element e : html.getElementsByTag("a")) {
if (!"div".equalsIgnoreCase(e.parentNode().nodeName())) {
System.out.println(e.attr("href"));
}
}
Here I am checking that the parent node of the anchor element is not div. if it is not div I am printing the url.
I have a multi-select Box and I'm doing some javascript to sort the order of the elements in the box. I want to submit the whole array after sorting back to the Java and not just the select items. How can I achieve this?
JSP:
<script type="text/javascript">
$(document).ready(function(){
$("#mup")[0].attachEvent("onclick", moveUpItem);
$("#mdown")[0].attachEvent("onclick", moveDownItem);
});
function moveUpItem(){
$('#list option:selected').each(function(){
$(this).insertBefore($(this).prev());
});
}
function moveDownItem(){
$('#list option:selected').each(function(){
$(this).insertAfter($(this).next());
});
}
</script>
<table width="100%">
<tr>
<td width="50%" align="center">
<h1>DET Column Maintenance</h1>
</td>
</tr>
</table>
<form action="process.det_column_order" method="post" name="detColumnSortorder" >
<table class="data_table">
<tr align="center">
<td>
<select id="list" name="fieldNames" size="35" multiple="multiple" style="width: 250px;">
<c:forEach var="field" items="${detFields}">
<option value="${field.fieldName}">${field.displayName}</option>
</c:forEach>
</select>
</td>
<tr>
<td style="text-align: center;">
<button id="mup">Move Up</button>
<button id="mdown">Move Down</button>
</td>
</tr>
<tr>
<td style="text-align: center;">
<input name="action" type="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
FORM:
private String[] fieldNames;
public String[] getFieldNames() { return this.fieldNames; }
public void setFieldNames(String[] val) { this.fieldNames = val; }
Since forms only submit the selected values, you'll need a little more JS and another form field.
Introduce a hidden form field that will hold the values you care about:
<input type="hidden" name="fieldNamesOrder" id="fieldNamesOrder"/>
And after every click of Move Up/Move Down:
var order = [], sel = document.getElementById("list");
for(var i = 0, len = sel.options.length; i < len; i++) {
order.push(sel.options(i).value);
}
document.getElementById("fieldNamesOrder").value = order.join(",");
Then, on the server side, you can read your ordered field names out of that posted field.
You either need to create a hidden text field in your HTML form that stores the values from each of your options... or you need to programatically select all of the options before the form is submitted.
I have written a javascript function.
function wellChecked(state) {
if (state)
{
wellDropDown.style.visibility = 'visible';
}
else
{
wellDropDown.style.visibility = 'hidden';
}
}
I have a checkbox after the Well Modification <td> as given below,
<tr>
<td>On Call</td>
<td><html:checkbox property="onCall"/></td>
<td>Well Modification</td>
<td><input type="checkbox" onclick="wellChecked(this.checked)" /></td>
</tr>
When that checkbox is clicked I want the drop down list given under the div id=wellDropDown to be displayed. Defaultly, if the check box is not clicked, the drop down should not be displayed.
<tr>
<td>Active</td>
<td><html:checkbox property="active"/></td>
<div id="wellDropDown" style="visibility:hidden;">
<td>
<html:select property="wellFormatId">
<html:option value="">(Select)</html:option>
<bean:define id="wellFormatColl" name="wellFormats" scope="request"/>
<logic:iterate id="wellFormats" name="wellFormatColl" indexId="index" type="com.astrazeneca.compis.data.WellFormatVO">
<% Long wellId = wellFormats.getWellFormatId();%>
<% String wellIdNo = wellId.toString(); %>
<html:option value="<%=wellIdNo%>">
<bean:write name="wellFormats" property="wellFormatName"/>
</html:option>
</logic:iterate>
</html:select>
</td>
</div>
</tr>
When I tried executing this code, I could see the drop down list getting displayed irrespective of the checkbox checked or not.
Where I have went wrong in this scenario? Please give ur suggestions or ways to implement my requirement.
Your HTML is invalid. You may not have a div enclose a td like this. Either make the td itself visible or invisible, or put the div inside the td, instead of putting it around the td.
Also, unless wellDropDown is a global JS variable, the code should be
document.getElementById("wellDropDown").style.visibility = 'visible';
with jquery you could do this :
<tr>
<td>On Call</td>
<td><html:checkbox property="onCall"/></td>
<td>Well Modification</td>
<td><input type="checkbox" id="myCheckBox" /></td>
</tr>
...
<script>
$('#myDropDown').click(
function () {
$("#wellDropDown").toggle();
});
);
</script>