jquery .find/.closest/.next - javascript

I have a table with one row having 4 cells as below
<table class="Header3" style="width: 100%;">
<tr>
<td id="RC" class="contLabel" style="width: 228px">
<div class="abc">Related Companies</div>
</td>
<td style="width: 101px">
<asp:checkbox runat="server" id="RCR" type="checkbox" ENABLED="false"/>
</td>
<td style="width: 116px">
<asp:checkbox runat="server" id="RCP" type="checkbox" ENABLED="false"/>
</td>
<td id="RC2" class="contLabel">Are related companies used to improve results?</td>
</tr>
And I below the table, i have a textbox inside div, the code is below:
<div id="RCN" class="panel2" style="width: 100%;">
<div class="ContentTB">
<asp:TextBox id="RCNT" runat="server" TextMode="MultiLine" width="99.5%" Enabled="false"></asp:TextBox>
</div>
</div>
What I want to achieve is the moment I click the first cell (#RC) or the 4th cell (#RC2), the textbox below the table get toggled, currently my jquery code is
$(function() {
$('#RC,#RC2').click(function(e) {
$('#RCN').toggle('fast');
});
})
and it works ok.
Now problem is that I have 20 tables and 20 textboxe, and I don't want to write 20 lines jquery code.
I'm trying things like
$('.contLabel').click(function(e) {
$(this).next('.panel2').toggle('fast');
});
but it does not work. I wonder what's the correct code?
The reason that I don't put all of them in one table is that the toggle animation does not look good.
Thanks for any advice!

You'd want to go back up to the table using closest() then use next() to get the div with the textarea in it.
$('.contLabel').on('click', function (e) {
var $td = $(this);
$td.closest('table').next('.panel2').toggle('fast');
});

This should do the trick
$('.contLabel').click(function (e) {
$(this).parent('tr').parent('tbody').parent('table').next('.panel2').toggle('fast');
});

Related

Radio Button in Asp.net Webform for multiple choice question

I am creating multiple-choice questions in ASP.NET Web Forms. I am getting the question and multiple choice options from the database. Is there a simple way to hide the 3rd option (C) when there are only two inputs (True or False)? Thank you in advance!
Here is aspx code:
<tr>
<td class="style7">A.</td>
<td style="text-align: left">
<asp:RadioButton ID="RbA" runat="server" GroupName="gtog"/>
</td>
</tr>
<tr>
<td class="style7">B.</td>
<td style="text-align: left">
<asp:RadioButton ID="RbB" runat="server" GroupName="gtog" />
</td>
</tr>
<tr>
<td class="style7">C.</td>
<td style="text-align: left" >
<asp:RadioButton ID="RbC" runat="server" GroupName="gtog" />
</td>
</tr>
As we have no info as to what is happening on your backend I'll keep this generic.
Add an id and runat="server to the final table row. You can then access that serverside to hide an show the row.
ASPX
<tr id="rowC" runat="server">
<td class="style7">C.</td>
<td style="text-align: left" >
<asp:RadioButton ID="RbC" runat="server" GroupName="gtog" />
</td>
</tr>
C#
if(answerCount == 2)
{
rowC.visible = false;
}
On a side note, you really should avoid using tables for layout. It is now 2020 there are many better ways to layout a form (or anything).
Yes, you can check if the item from the database is of Type bool - if this is true make 3rd Options(C) hidden.
So create a variable that checks the item from the database, if its a Boolean then add hide RbC.
Type b = itemFromDB.GetType();
if(b is bool)
RbC.hidden = true;

How to display text in multiple lines in cloned asp:textbox?

I have a div and I have created its clone on a button click. I changed the id of the cloned object and the textbox inside it. Now I want to add text in the cloned textbox. How can I add text into an asp:textbox in multiple lines?
The problem I am getting is, if I use textmode=multiline, nothing is being displayed and if I remove textmode=multiline then text is displaying in the same line.
Please find below my code.
var cloneCount = 1;
$(document).ready(function () {
$("#click").click(function () {
cloneCount = cloneCount + 1;
var clone = $("#dynamicDiv").clone(true);
clone.show();
clone.find('input[type="text"]').attr('id', 'textbox' + cloneCount);
clone.find('input[type="text"]').val("yasir \r\n yasir");
clone.css('display', 'inline-block');
clone.attr('id', 'dynamicDiv' + cloneCount).insertAfter("#dynamicDiv");
clone.appendTo("#divHolderFirstModule");
});
});
$(function () {
$(document).on('click', '.removeDiv', function () {
$(this).closest('div').remove();
});
});
<div ID="dynamicDivFirstModule" >
<%-- Dynamic Div--%>
<div id="dynamicDiv" style="display:none; border:solid;">
<table>
<tr>
<td class="auto-style1" rowspan="2">
<asp:TextBox ID="textbox" TextMode="MultiLine" runat="server" ReadOnly="true" style="overflow:auto;" Height="102px" Width="235px"></asp:TextBox>
</td>
<td id="hoverHere">
<asp:Label ID="Hovar" runat="server" Text="?"></asp:Label>
<p id="hover" class="masterTooltip" onmouseover="Hover();">?</p>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Delete" runat="server" Text="Delete" CssClass="removeDiv"/>
</td>
</tr>
</table>
</div>
<%-- Dynamic Div--%>
</div>

remove item from listview using jquery or javascript

I have the below listview and I want to remove an item when the user presses the add to cart button. I am not 100 percent sure how the listview works because ID's have to be unique and as you can tell the table row with an ID of gameRow will not be unique. What I am wanting to achieve is to remove all of the tr's inside of that gameRow when the user presses the add to cart button.
I am using a asp listview and populating this through the selectmethod.
<asp:ListView ID="lvProductListing"
ItemType="CommonBusinessObjectsGS.Game"
SelectMethod="GetGames"
DataKeyNames="GameId"
runat="server" EnableViewState="false" >
<LayoutTemplate>
<table id="productListingTable" class="productLisingTable">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="gameRow">
<tr>
<td rowspan="6">
<img class="gameImageIdClass" id="gameImageId" src="<%#:Item.GameImageString%>" alt="" />
</td>
</tr>
<tr>
<td><%#:Item.GameName%></td>
</tr>
<tr>
<td><%#:Item.PlatformName%></td>
</tr>
<tr>
<td><%#:Item.ConditionShortDescription%></td>
</tr>
<tr>
<td><%#:Item.RetailCost.ToString("c")%></td>
</tr>
<tr>
<td>
<button id="btnAddToCart" name="btnAddToCart" onclick="removeGameRow()" value="<%#:Item.GameId %>">Add to Cart</button>
</td>
</tr>
</tr>
</ItemTemplate>
</asp:ListView>
I ended up changing the html to make this a bit easier. Here is what my ItemTemplate for my listview now looks like. With some help with CSS (below) the gameImageDiv and gameInfoDiv will be side by side.
<ItemTemplate>
<div id="gameContainerDiv">
<div id="gameImageDiv">
<img class="gameImageIdClass" id="gameImageId" src="<%#:Item.GameImageString%>" alt="" />
</div>
<div id="gameInfoDiv">
<ul id="gameInfoUL">
<li><%#:Item.GameName%></li>
<li><%#:Item.PlatformName%></li>
<li><%#:Item.ConditionShortDescription%></li>
<li><%#:Item.RetailCost.ToString("c")%></li>
<li><button type="button" id="btnAddToCart" name="btnAddToCart" onclick="removeGameRow(this)" value="<%#:Item.GameId %>">Add to Cart</button></li>
</ul>
</div>
</div>
</ItemTemplate>
Now for the CSS to stack the game information beside the image of the game.
#gameContainerDiv {
display: table;
}
#gameImageDiv, #gameInfoDiv {
display: table-cell;
}
#gameInfoUL{
list-style-type:none;
}
Now for the jQuery to remove the game image and the info about that game. The onclick event will execute the removeGameRow function passing in the button that was clicked. From that button I am getting the button id and I am finding the closest div with a class name of gameContainerDiv and removing it. gameContainerDiv - is the container for the game image and the info about the game.
$(document).ready(function () {
function removeGameRow(button) {
$(button).closest("#gameContainerDiv").remove();
};
});

show/hide textbox based on yes/no

I have a survey, 30+ yes/no questions(radiobutton); if choose no, show textbox to give reason, and this textbox is required if choose no. I can't do it one by one. How to use wildcard and loop to do this.
<tr>
<td>1</td>
<td width="600px">question 1?</td>
<td width="65px">
<asp:RadioButton ID="rdM1Y" runat="server" Text="Yes" GroupName="rdM1" />
<asp:RadioButton ID="rdM1N" runat="server" Text="No" GroupName="rdM1" />
</td>
<td><asp:TextBox ID="txtCM1" runat="server" /></td>
</tr>
<tr>
<td>2</td>
<td width="600px">question 2?</td>
<td width="65px">
<asp:RadioButton ID="rdM2Y" runat="server" Text="Yes" GroupName="rdM2" />
<asp:RadioButton ID="rdM2N" runat="server" Text="No" GroupName="rdM2" />
</td>
<td><asp:TextBox ID="txtCM2" runat="server" /></td>
</tr>
I need validate those textboxs isrequired if they choose no
ok, so:
include jQuery (makes it all way easier):
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
add radNo class for all "NO" radio buttons:
<asp:RadioButton class="radNo" ID="rdM2N" runat="server" Text="No" GroupName="rdM2" />
add txtNo class for all textboxes:
<asp:TextBox class="txtNo" ID="txtCM2" runat="server" />
add script:
<script type="text/javascript">
$(function(){
$('.radNo').click(function(){
var c = $(this).is(':checked');
$(this).parents('tr').find('txtNo').toggle(c);
if (c) {
$(this).parents('tr').find('txtNo').focus();
}
});
// validate that there's text if select NO
$('.txtNo').bind('blur', function(){
if ($(this).val().length < 1) {
$(this).focus();
alert('please provide a reason');
}
});
});
</script>
that should do it, hope i understood well and it's helpful.
Generated HTML - note the textarea ID equals the radio button name
<table>
<tr>
<td>1</td>
<td width="600px">question 1?</td>
<td width="65px">
<label><input type="radio" name="rdM1" /> Yes</label>
<label><input type="radio" name="rdM1" class="give_reason" /> No</label>
</td>
<td><textarea id="rdM1" name="rdM1-reason"></textarea></td>
</tr>
</table>
CSS
textarea {display: none;}
Javascript
$(document).on('click', 'input:radio', function(){
el = $('#' + this.name);
($(this).hasClass('give_reason'))? el.show() : el.hide();
});
DEMO
First give no-radiobuttons a common class, for example say it "noRadio". Then, you can listen click events of all radio buttons with this class. Within the event handler function you can find the corresponding text box and show it. My sample code is below:
$(".noRadio").click(function(e) {
// get id of the clicked radio button
var id = $(e.target).attr("id");
// remove "rdM" and "N" from id and get pure question number
id = id.replace("rdM", "").replace("N", "");
// select the corresponding text box by its id, e.g. "#txtCM2" if "#rdM2N" selected
$("#txtCM" + id).show();
});
I tried this approach in my projects and it works.
$(document).on('click', 'input:radio', function(){
$('table').each(function(){
if($('input[type= radio]:checked').val() == no))
{
$('textbox').show();
}
});
});

Get Control values of datalist in javascript

I have a datalist and I am calling javascript on div click added in datalist ..
How can i get control values on click of my div added in datalist
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<div id="metroaqui" class="divsub" runat="server"
onclick="javascript:getvalues(this);">
<table>
<tr class="clstd">
<td>
<b>IP NO:-</b>
</td>
<td colspan="2" style="width: 20%">
<asp:Label runat="server" ID="ipnolable"
Text='<%# Eval("IpNo") %>'></asp:Label>
</td>
<td>
<b>PATIENT NO:-</b>
</td>
<td colspan="2">
<asp:Label runat="server" ID="Label1"
Text='<%# Eval("PatientNo") %>'>
</asp:Label>
</td>
</tr>
<tr>
</tr>
<tr>
</tr>
</asp:DataList>
To access the control inside the DataList, try to use the following way,
function getVal()
{
var datalist = document.getElementById('<%=DataList1.ClientID%>').childNodes[0];
var tb = datalist.getElementsByTagName("input");
for (var i=0;i<tb.length;i++)
{
//TextBox
if (tb[i].type=="text")
{
alert(tb[i].value);
}
//DropDown
if (tb[i].type=="select")
{
alert(tb[i].options[tb[i].selectedIndex].value);
}
}
}
You can simple use document.getElementById to get the values from your controls.
Note that you id's will change at runtime.
Also remember that label render as span on browser so you will have to check for innerText and innerHtml of your control.
You can take benefit of ClientIDMode="static" for static client id of the server controls. like asp:Label etc.

Categories