Reputation: 404
I am currently creating a backend. What I need is some jquery to insert some different form fields. I can for example have this code:
<div>
<div>
Name
</div>
<div>
Type
</div>
<div>
<a class="edit" href="#">Edit</a>
</div>
</div>
This code needs to get converted so the Name and Type text is insted the values of two input fields when I click a.edit
EDIT:
I've tried this code - but when I click "a.ff-save" it does not alert??? What is my problem?
<script type="text/javascript">
$(document).ready( function() {
var this_form_id = $("input#form_id").val();
$("#formfield-list div.formfield-ff:odd").css('background', '#f6f6f6');
$("a.ff_add").click( function() {
var name_val = $("input#new_field_name").val();
var type_val = $("input#new_field_type").val();
var demand_val = $("input#new_field_demand").val();
$.ajax({
type: "POST",
url: "{pref_folder}/admix/forms/addFormField",
data: ({ form_id: this_form_id, field_name: name_val, field_type: type_val, field_demand: demand_val }),
success: function(text) {
$.ajax({
type: "post",
url: "{pref_folder}/admix/forms/getFormFields",
data: { form_id: this_form_id },
success: function(t) {
$("#formfield-list").empty().append(t);
$("#formfield-list div.formfield-ff:odd").css('background', '#f6f6f6');
},
dataType: "html"
});
$(".add-succes-message").empty().append('<div class="msg">'+text+'</div>');
$(".add-field-row").slideUp(500, function(){
$(".add-field-row").slideDown(500);
}).delay(3500);
$(".add-succes-message").slideDown(500, function(){
$(".add-succes-message").slideUp(500);
}).delay(3500);
},
cache: false,
async: false,
dataType: "html"
});
});
$("a.ff-save").click(function (){
alert("ok");
});
$("a.ff-edit").click(function (){
var fid = $(this).prev().val();
var nameValue = $("#ff-"+fid+" div.ff-name").html().trim();
var typeValue = $("#ff-"+fid+" div.ff-type").html().trim();
var demandValue = $("#ff-"+fid+" div.ff-demand").html().trim();
var typeInt;
if( typeValue == "Tekstfelt, 1 linje" )
{
typeInt = 1;
} else {
typeInt = 0;
}
$("#ff-"+fid+" div.ff-name").html('<input type="text" name="name" class="jq-input" value="'+nameValue+'" />');
$("#ff-"+fid+" div.ff-type").html('<input type="text" name="type" class="jq-input" value="'+typeInt+'" />');
if( demandValue == "Ja")
{
$("#ff-"+fid+" div.ff-demand").html('<input type="checkbox" name="demand" class="jq-input" value="" checked="checked" />');
} else {
$("#ff-"+fid+" div.ff-demand").html('<input type="checkbox" name="demand" class="jq-input" value="" />');
}
$("#ff-"+fid+" .ff-edit, #ff-"+fid+".ff-delete").hide();
$("#ff-"+fid+" div:last").html('<a href="#" class="ff-save">Save</a>');
});
$("a.ff-delete").click();
});
</script>
Upvotes: 1
Views: 16777
Reputation: 851
I tried a few different approaches and this is what I came up with:
HTML:
<span class="subject">
<span class="text"> subject </span>
<input class="input" type="text" name="subject" val="subject" hidden/>
</span>
Jquery:
function showInput(){
$(this).off('click');
$(this).siblings('input').each(function(){$(this).show();});
$(this).siblings('input').focus();
$(this).hide();
}
function hideInput(){
$(this).hide();
$(this).siblings('span').show();
$(this).siblings('span').click(showInput);
}
$(function(){
$('.text').click(showInput);
$('.input').blur(hideInput);
}
Simply put when you click on the given text it will turn into an input box and when the input box loses focus then it turns back into regular text.
I also made the show/hide functions separate callable functions so I could reset the click listener within the hideInput function.
Upvotes: 7
Reputation: 82933
Check this jQuery plugin. It exactly does what you are looking for.
If you are not inclined towards using the plugin, then try the code below:
<div>
<div>
<label class="editable">Name</label>
<input class="editable" type="text" name="name" style="display:none"/>
</div>
<div>
<label class="editable">Type</label>
<input class="editable" type="text" name="type" style="display:none"/>
</div>
<div>
<a class="edit" href="#">Edit</a>
</div>
</div>
<script type="text/javascript">
$(function(){
$(".edit").click(function(){
var $this = $(this);
var text = $this.text();
if(text=="Edit"){
$this.text("Cancel");
}
else{
$this.text("Edit");
}
$(".editable").toggle();
});
$("input.editable").change(function(){
$(this).prev().text($(this).text());
});
});
</script>
EDIT:
To bind the click event handler to the anchor element use the live function of jQuery. e.g:
$("a.ff-save").live("click", function (){
alert("ok");
});
Upvotes: 7
Reputation: 13976
I did something like this at work, here was my solution:
HTML
<div id='container'>
<div id='name' class='text'>
Name
</div>
<div id='type' class='text'>
Type
</div>
<div>
<a class="edit" href="#">Edit</a>
</div>
</div>
JQuery
function StartEditState() {
var container = $('#container');
container.find('a.edit').unbind('click').click(SaveEditState).html('Save'); //this changes edit button save button
container.find('div.text').each(function() {
$(this).html('<input class="new_val" type="text" value="' + $(this).html() + '" /><input class="orig_val" type="hidden" value="' + $(this).html() + '" />'); // this creates a hidden with the old value and an input that they can change
});
return false;
}
To save the values you just need to do this $('#name input.new_val').val()
(for name, use #type
for type) or to return to the original values use $('#name input.orig_val').val()
Hope you like it.
Upvotes: 1