tags:

views:

37

answers:

3

Okay, I might confuse some people who might be able to answer this question, I'll set this in the manner that I understand. Please bear with me.

Let's say I have an array of divs in this manner:

<div id="par1"><span>Text 1</span></div>
<div id="par2"><span>Text 1</span></div>
<div id="par3"><span>Text 1</span></div>
<div id="par4"><span>Text 1</span></div>

Then, I have a hidden div with a textbox that acts like an editor:

<div id="editor" style="display: hidden"><input id="uniqueID$textbox" type="text" /></div>

What I want to achieve in jQuery is to visually display the #editor div within the divs above when i click each div somewhat an editor. Instead of having seperate input boxes within those divs and displaying them when needed, I intend to just use one textbox for each.

I hope I explained my problem well and hope you guys can help

A: 

You can clone the div with id editor and put inside the clicked divs like this:

var $clone = $('#editor').clone();

$('div[id^="par"]').click(function(){
  $(this).append($clone);
});

See the working example here

Sarfraz
clever, but i wouldn't want that since that will duplicate the ID's. any other approaches? thanks man!
Martin Ongtangco
@Martin Ongtangco: Which element's id are you talking about?
Sarfraz
Maybe i haven't explained my problem thoroughly. my apologies. I'm using telerik controls where in i can't just re-generate a control (like a datepicker) via javascript. Those set of controls generate unique ID's to each element (input DOMs), so appending it won't work as what a normal input box will, based on your example.
Martin Ongtangco
+1  A: 

You might want to replace the <span> with an <input> element on click, like

$(function(){
  $('span').bind('click', function(){
     $(this).replaceWith($('<input/>', {
        id:       'newinp',
        type:     'text',
        focusout: function(){
           $(this).replaceWith('<span>' + $(this).val() + '</span>');
        }
     }));       
     $('#newinp').focus();
  });
});​

Working example: http://www.jsfiddle.net/qbDKN/

jAndy
+1  A: 

You can't avoid re-rendering HTML but I suspect you simply meant re-rendering the entire page. Certainly possible in JS, in jquery I believe you want a combination of detach() and one of the append() / prepend() / *pendTo() methods as per the documentation example here.

annakata
hmm... ill try this @annakata.
Martin Ongtangco
it kills the telerik control. are there other ways?
Martin Ongtangco
This is the crux of the problem with asp.net webforms - the damned thing oversteps it's bounds and you have to do everything it's way or not at all. I suggest you wrap all of this in an updatepanel, there's no other way I know of to manipulate part of a page and maintain the asp.net view model.
annakata