How to differentiate and replace the span text on mouse over - jquery-ui

I have a table structure like this with out any ID and span has same text.
<table>
<tr>
<td>
<span>Name</span>
</td>
<td>
<span>Name</span>
</td>
</tr>
</table>
If i click on first span this will open a pop up. Pop up contains text box and Button.If i type some text and click the button, the clicked span should replace with newly entered text. how can i identify the clicked span and replace the text?.
The same way we need to do for second span.
Thanks

you can pass this to your function when you click on span like so:
<table>
<tr>
<td>
<span onclick="openPopup(this);">Name</span>
</td>
<td>
<span onclick="openPopup(this);">Name</span>
</td>
</tr>
</table>
You will have to store the element so u can change it later when ure done with the popup.
function openPopup(el)
{
// open popup and if button pressed
el.innerHTML = yourNewText;
}

Here is a simple example of how to achieve this:
http://jsfiddle.net/ApsbC/7/
And the code snippet:
$(document).on("click","span",function(){
alert($(this).text());
// some popup code here
$(this).html("Some new Name");
});
EDIT:
I suggest though putting some class/id on those spans to have a better selector for the .on() method. So instead of having just span you could have something like span.spanClass

Try this:
<table>
<tr>
<td>
<span onclick="openPopup(this);">Name</span>
</td>
<td>
<span onclick="openPopup(this);">Name</span>
</td>
</tr>
</table>
<script type="text/javascript">
function openPopup (o) {
var newText = prompt("Enter some text: ");
$(o).text(newText);
}
</script>

if you add a click event in your span, you will have the tag reference with "this". Then if you call .text() on it, the related tag will be updated.
like that :
$('span').click(function() {
$(this).text('new text');
})
Here is an example using jQuery Dialog widget to edit span text
http://jsfiddle.net/npch3/2/

Related

Close all other jQueryUI tooltips when a new tooltip is opened

How can I close all other jQueryUI tooltips when a user opens a new tooltip. I want to avoid littering the UI with open tooltips.
I did not want to clutter up what I thought was a straightforward question but my tooltips are customized to only show when a user has clicked on a help icon or field name as in the example below. In addition as in the example, the help triggers are not in the [label] tag associated to that input and so the tooltip can't count on the field focus. I suspect that is the issue.
function loadCSS(filename) {
var file = document.createElement("link");
file.setAttribute("rel", "stylesheet");
file.setAttribute("type", "text/css");
file.setAttribute("href", filename);
document.head.appendChild(file);
}
loadCSS("https://code.jquery.com/ui/1.12.1/themes/start/jquery-ui.css");
// Disable HOVER tooltips for input elements since they are just annoying.
$("input[title]").tooltip({
disabled: true,
content: function() {
// Allows the tooltip text to be treated as raw HTML.
return $(this).prop('title');
},
close: function(event, ui) {
// Disable the Tooltip once closed to ensure it can only open via click.
$(this).tooltip('disable');
}
});
/* Manually Open the Tooltips */
$(".ui-field-help").click(function(e) {
var forId = e.target.getAttribute('for');
if (forId) {
var $forEl = $("#" + forId);
if ($forEl.length)
$forEl.tooltip('enable').tooltip('open');
}
});
.ui-field-help {
text-decoration: underline;
}
input {
width: 100%
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
<table width=100%>
<tr>
<td for="A000" class="ui-field-help">First</td>
<td><input type="Text" id="A000" title="title #A000"></td>
</tr>
<tr>
<td for="B000" class="ui-field-help">Second</td>
<td><input type="Text" id="B000" title="title #B000"></td>
</tr>
<tr>
<td for="C000" class="ui-field-help">Third</td>
<td><input type="Text" id="C000" title="title #C000"></td>
</tr>
<tr>
<td for="D000" class="ui-field-help">Fourth</td>
<td><input type="Text" id="D000" title="title #D000"></td>
</tr>
<tr>
<td for="E000" class="ui-field-help">Fifth</td>
<td><input type="Text" id="E000" title="title #E000"></td>
</tr>
</table>
What I was asking for was unnecessary. If used correctly jQueryUI tooltips will automatically close themselves based on the focus/hover events.
If the tooltip is opened without triggering these events then there won't be any way for it to automatically close itself.
I had intentionally opened the tooltips via a click on a separate help icon. That click did not trigger the focus. By fixing my code to move the icon into the LABEL (for=) tied to the INPUT, it resulted in the INPUT receiving the focus which then gave the jQueryUI tooltip widget what it needed to manage the visibility.

Reorder items of an angularfire backed list, drag and drop style

I'm building an angularjs / firebase app unsing the angularfire bindings (v0.5.0).
I have a list of items, displayed in a table with ng-repeat on the <tr>, something like this:
<table>
<tbody>
<tr ng-repeat="(index, item) in items">
<td>
<input type="checkbox" ng-model="item.done">
</td>
<td>
<input type="text" ng-model="item.text" ng-focus="onItemFocus(index)" ng-blur="onItemBlur(index)">
</td>
<td>
<button type="button" ng-click="remove(index)">×</button>
</td>
</tr>
</tbody>
</table>
and there's an angularfire 3 way data binding on this list of items, something like:
$scope.ref = new Firebase('...');
$scope.remote = $firebase($scope.ref);
$scope.remote.$child("items").$bind($scope, "items");
This works all fine, but now I'm trying to add the possibility to reorder the items with drag and drop.
I managed to setup the drag and dropping UI with jquery-ui (essentially calling $("tbody").sortable()), but my problem is to bind it to the angular models. There's a number of questions regarding that (with great jsfiddles) but in my case the angularfire 3 way binding seems to be messing it up.
I think I need to use firebase priorities with angularfire's orderByPriority and maybe deal with it in one of the sortable callbacks but I'm having trouble figuring out exactly how I should do that... and can't find any sort of documentation about it.
Has anyone done something similar, and could you share some pointers on how to set this up?
I saw your post a long time ago while I was looking for the same solution. Here is something I put together:
function onDropComplete(dropIndex, item) {
if (!item.isNew){
var dragIndex = $scope.fireData.indexOf(item);
item.priority = dropIndex;
$scope.fireData.$save(dragIndex);
if (dragIndex > dropIndex){
while ($scope.fireData[dropIndex] && dropIndex !== dragIndex ){
$scope.fireData[dropIndex].priority = dropIndex+1;
$scope.fireData.$save(dropIndex);
dropIndex++;
}
} else if(dragIndex < dropIndex){
while ($scope.fireData[dropIndex] && dropIndex !== dragIndex ){
$scope.fireData[dropIndex].priority = dropIndex-1;
$scope.fireData.$save(dropIndex);
dropIndex--;
}
}
} else if (item.isNew){
item = angular.copy(item);
item.isNew = false;
item.priority = dropIndex;
$scope.fireData.$add(item);
while ($scope.fireData[dropIndex]){
$scope.fireData[dropIndex].priority = dropIndex+1;
$scope.fireData.$save(dropIndex);
dropIndex++;
}
}
}
Here, I take items already in the list and have the priority properties of items adjust on drop, depending if the item that was dragged was above or below the drop area. Also, if the drag item in new to the list, it will be added at index where dropped and all items below will be bumped up 1 priority. This is dependent on having your list sorted by var sync = $firebase(ref.orderByChild('priority'));, and you to be using ngDraggable.
Here is some HTML for an example:
<tr ng-repeat="obj in fireData" ng-drop="true" ng-drop-success="onDropComplete($index, $data,$event)">
<td draggable="true" ng-drag="true" ng-drag-data="obj">
<span class="glyphicon glyphicon-move"></span><div class="drag-name">{{obj.name}}</div>
</td>
<td>{{obj.name}}</td>
<td>{{obj.type}}</td>
<td><input type="checkbox" ng-change="saveChanges(obj)" ng-model="obj.completed"></td>
<td>
<div class="" ng-click="deleteItemFromList(obj)">
<span class="glyphicon glyphicon-remove"></span>
</div>
</td>
</tr>

How to reset the partial view containing cascading dropdown list in MVC4 Jquery

I am using MVC4/EF and I have four cascading dropdownlist and I got it working the first time. So when the page is rendered first time, I am able to select the first dropdown and filter the result on the second and by selecting second dropdowm the third dropdwon is filtered and fourth and based on the fourth drop down I populate a edit view. But when I go back and change the selection in the first drop down, it is filtering the second drop down but not resetting the third, fourth dropdwon list and the edit partial view. Here is teh first dropdwon partial view code.
CampusUsercontrol.cshtml
#model RHMS.Models.RoomEditor
#using (Ajax.BeginForm("SelectCampus", "RoomEditor", new AjaxOptions { UpdateTargetId = "Buildings" }))
{
#Html.DropDownListFor(
m => m.SelectedCampusID,
new SelectList(Model.Campuses,"CampusId", "Name"),
string.Empty
)
}
<script type="text/javascript">
$('#SelectedCampusID').change(function () {
$(this).parents('form').submit();
});
</script>
Index.cshtml
#model RHMS.Models.RoomEditor
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<table cellpadding="0" cellspacing="4" border="0">
<tr>
<td>Campus </td>
<td> :</td>
<td>#Html.Partial("CampusUserControl", Model)</td>
</tr>
<tr>
<td>Building </td>
<td> :</td>
<td><div id="Buildings">#Html.Partial("BuildingUserControl", Model)</div></td>
</tr>
<tr>
<td>Floor </td>
<td> :</td>
<td><div id="Floor">#Html.Partial("FloorsUserControl", Model)</div></td>
</tr>
<tr>
<td>Room </td>
<td> :</td>
<td><div id="Room">#Html.Partial("RoomUserControl", Model)</div></td>
</tr>
</table>
<div id="RoomInfo">
#Html.Partial("RoomInfoUserControl", Model)
</div>
Please help me how to refresh the other partial views when the first one is changed.
Looks like this part of your javascript code is beeing executed only on the first load of the page
<script type="text/javascript">
$('#SelectedCampusID').change(function () {
$(this).parents('form').submit();
});
</script>
try wiring the change event for #SelectedCampusID using Jquery's on binding. You might also have to do the same for the other dropdowns.

How to get the textbox values which are generated dynamically using razor?

<tr>
#foreach (string s in "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15".Split(','))
{
<td>#Html.TextBox("Time","")
</td>
<p>
</p>
}
</tr>
How i get the values from textboxes created above using either javascript or jquery.
kindly assist me
Here it is:
$('input[name=Time]').each(function(index, element){
var txtValue = element.value;
});

ASP.NET MVC Data Validation - Highlight Table Row vs. TextBoxes

On an ASP.NET MVC View, I have a couple of checkboxes, one for email address and one for phone. I want to make sure that at least one is checked (both can be checked, so a radio button is not ideal) and if neither are, highlight the row with a red border just like a textbox is with the validation functionality...
I have other fields that are getting validated correctly and the CSS is changing when there is an issue on the textboxes and textareas accordingly. The code below displays the message informing the user they must specify a contact preference, but does not highlight the row as having an issue...
SCREEN SHOT
VIEW
<table width="100%">
<tr>
<td>
<label>
How would you like us to contact you?
</label>
</td>
</tr>
<tr id="pref_row">
<td>
<span class="bold-text">Email: </span>
<%=Html.CheckBox("EmailDesired")%>
<span class="bold-text">Phone: </span>
<%=Html.CheckBox("PhoneDesired")%>
</td>
</tr>
</table>
CONTROLLER
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(ContactUs contactus)
{
ContactUsService svc = new ContactUsService();
// Validation
if (!contactus.EmailDesired && !contactus.PhoneDesired)
ViewData.ModelState.AddModelError("pref_row", "Please specify a contact preference (phone and/or email)!");
if (ViewData.ModelState.IsValid)
{
MessageModel msg = svc.SendRequest(contactus);
return RedirectToAction("Index", msg);
}
else
{
return View();
}
}
When the HtmlHelper render itself it checks if there is any item in the ModelState dictionary that has the same key as the helper itself. if so the control will be rendered with the attribute class equal to "input-validation-error" which is defined in the css file.
So, the style will be applied only on the rendered input controls.
This is my solution:
<table width="100%">
<tr>
<td>
<label>
How would you like us to contact you?
</label>
</td>
</tr>
<tr class="<%=ViewData.ModelState["pref_row"]!= null ? "input-validation-error":"" %>">
<td>
<span class="bold-text">Email: </span>
<%=Html.CheckBox("EmailDesired")%>
<span class="bold-text">Phone: </span>
<%=Html.CheckBox("PhoneDesired")%>
</td>
</tr>
</table>

Resources