views:

52

answers:

2
 <script>
function edit(elem)
{
var ele=$(elem).siblings('label#test').html();
var a=document.getElementById('test');
var htm = '<input type="text" name="modal" id="modal" style="width:70%;" value="'+$(elem).siblings('label#test').html();+'"/>';
$dialog.html(htm)
     .dialog({
        autoOpen: true,
        position: 'center' ,
        title: 'EDIT',
        draggable: false,
        width : 300,
        height : 40,
        resizable : false,
        modal : true,

        buttons: { "Save" : function() { if($('#modal').val() != ''){a.value=$('#modal').val();$dialog.dialog('close');} else {alert('i++'+$('#modal').val()+'++');} } }
     });
$dialog.dialog('open');
  }


 $(document).ready(function() {
  var flag=0;

  $("input:radio[@name=template]").click(function() {
     var checkedoption = $(this).val();
     if (checkedoption == "template3")
     {
        if (flag == 0)
        {
           var html = '<input type="text" name="val" id="val" />&nbsp;&nbsp;&nbsp;<input type="button" value="Add" id="add"><br>';
           $('#useroptions').append(html);
           flag=1;

           $('#add').click(function(){
              var a = $('#val').val();
              if (a == '')
              {
                 alert('Enter options');
              }
              else
              {
                      var section= '<tr class="clickable"><td id="userval" BGCOLOR="#FF6699"><label id="test">' + a + '</label>&nbsp;&nbsp;&nbsp; <IMG SRC="/media/img/chunkedit.gif" onclick="javascript:edit(this);" >&nbsp;&nbsp;&nbsp;<IMG SRC="/media/img/close.gif" onclick="javascript:remove(this);" ></td></tr>';
                  $('#useroptions').append(section);
              }
     });
     }
 }
});
});
</script>
<form>
<table>
       <tr><td>
<div id="useroptions"></div>
</tr></td>

</table>
 </form>

How to set a new value for test in the above code.. Thanks

+2  A: 

Use the value overload like this:

$(elem).siblings('label#test').html("<span>new value</span>");
Nick Craver
But it will change the old value in this case rit?
Hulk
@Hulk, that's correct, can you describe more of what you're after? I'm sure we can provide a better solution to your overall problem.
Nick Craver
Hi,If i add 2 values say a and b and then set the html to the above said it will overide 'a' since 'a' was added first while i edited 'b'.Hot to deal this..
Hulk
@Hulk - That would mean 2 siblings are present with ID `test`...that's invalid HTML. If `a` and `b` are on different rows, that's ok, the `.siblings()` call won't leave the row. However, the multiple ID issue would still be there, I recommend using a class instead.
Nick Craver
K.Can u please show me a demo code by adding the class.
Hulk
@Hulk - Could you update your post with the table you're trying to edit?, it's easier to show you want to change than to guess your structure.
Nick Craver
Well Nick i have given u the whole code..
Hulk
+1  A: 

You can just refer to #test as id-s need to be unique and then set the value using text (or use html if you need to create html elements inside #test)

$('#test').text("new value");
Mario Menger
But it will change the old value in this case rit?
Hulk
Yes, it will overwrite the old value. If you want to keep the old value, you have to store that somewhere.
Mario Menger
Hi,If i add 2 values say a and b and then set the html to the above said it will overide a since a was added first while i edited b.Hot to deal this..Hi,If i add 2 values say 'a' and 'b' and then set the html to the above said it will overide 'a' since 'a' was added first while i edited 'b'.Hot to deal this..
Hulk