0
<div class="main">
<button class="btnClick" type="button"> </button>
<button class="btnClick" type="button"> </button>
<div class="child">
<p> ** Some child Text ** </p>
<textarea id="edit" class="hidden"></textarea>  //class hidden == Display:none
</div>
</div>

<div class="main">
<button class="btnClick" type="button"> </button>
<button class="btnClick" type="button"> </button>
<div class="child">
<p> ** Some child Text ** </p>
<textarea id="edit" class="hidden"></textarea>  //class hidden == Display:none
</div>
</div>

Am trying to implement the edit section with the above code. The above <div> is generated dynamically and when user click the button i need to hide the <p> "** Some child Text ** " and make the Textarea visible for that particular Div and copy the

section to Textarea

I tried On Button Click

$(this).closest('#edit').removeClass("hidden");

but it didn't worked and with the other code every time only first textarea is visible irrespective of which button i click

What am looking for is on the first button click the particular <div> textarea class hidden to be removed and copy the<p> text to Textarea on that particula <div>

What is the best way to do this. and i can add any new class and make code generic. Your ideas !!

3 Answers 3

2

you should remove the id from the textarea to avoid having multiple id's which are the same.

This function does what you need.

$(document).on('click', '.btnClick', function(){
    //cache the child div block (everything you want is in here)
    child = $(this).next('.child');

    //hide the p tag
    child.find('p').hide();

    //this copies the p tag in
    child.find('textarea').val( child.find('p').html() ); 

    //this shows the textarea
    child.find('textarea').show();
});

here is a jsfiddle: http://jsfiddle.net/6wAUK/

Sign up to request clarification or add additional context in comments.

4 Comments

Fiddle looks great !! how do i navigate to div if i have 2 buttons on first button click
I don't follow what you mean?
updated code in my question another button added and i want the event to happen on first button click
The easiest way would be to assign another class to the first button & run the jQuery against that. Otherwise you could do $('.main').find('.btnClick').first().click( function() { ... } ); You could copy the function I'm here.
1

Try

$(document).on('click', '.btnClick', function(){
    var div = $(this).next()
    div.find('p').hide();
    div.find('textarea').show();
})

1 Comment

Thanks! some minor change in my HMTL.Updated Code
0

Try

$(this).parent("div").find('#edit').removeClass("hidden");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.