Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Change text-input to textarea

    Dear Coders,

    I'm having a little problem converting the below code from <input type="text"> to <textarea></textarea> because it's inside a java code, that I don't quite understand.

    Code:
    $('<input type="text">').val(container.text()).appendTo(container.empty());
    I guess this ".find" function needs to be changed as well:

    Code:
    $('.todo a.saveChanges').live('click',function(){
    		var text = currentTODO.find("input[type=text]").val();
    ...

    Please let me know if you need to see more of the script in order to help

    Thanks in advance.

    Morten

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Try to do that:
    Code:
    $('<textarea>').val(container.text()).appendTo(container.empty());
    and
    Code:
    $('.todo a.saveChanges').live('click',function(){
    		var text = currentTODO.find("textarea").val();
    ...

  • #3
    New Coder
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you for the answer.
    Where/how do i end the textarea tag?

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    It kind of ends itself :-)

    This code
    Code:
    $('<textarea>').appendTo(...);
    will create a complete <textarea> and append it to the destination. This complete tag will include the closing tag.

  • Users who have thanked devnull69 for this post:

    walkie (04-27-2011)

  • #5
    New Coder
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Aaah cool!
    Because I tried myself with this:

    Code:
    $('<textarea>').val(container.text()).appendTo(container.empty()).('</textarea>');
    And i kinda knew it was wrong, but worth the try hehe.

    I can add a parameter to the textarea inthere right? Like this:

    Code:
    $('<textarea class="name">').val(container.text()).appendTo(container.empty());

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Yes, you can

  • #7
    New Coder
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks alot for you help mate.

    Would you mind if I PM you if I have some other small problems with jQuery? You seem to know your way around it, and i wont abuse it.
    Or would you prefer if I make new posts?

  • #8
    New Coder
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts
    So the textarea is now showing, but there's a problem with saving the text now.
    When i press save, the new text shows, but it does not update the MySQL. Because when I press refresh, it disappears.

    Here's what i have.

    The save function:
    Code:
    	$('.todo a.saveChanges').live('click',function(){
    		var text = currentTODO.find("textarea").val();
    		
    		$.get("ajax.php",{'action':'edit','id':currentTODO.data('id'),'text':text});
    		
    		currentTODO.removeData('origText')
    					.find(".text")
    					.text(text);
    	});

    Which leads to the "edit case" in ajax.php:
    Code:
    case 'edit':
    	ToDo::edit($id,$_GET['text']);
    	break;

    That calls the function for updating the database:
    Code:
    public static function edit($id, $text){
    
    	$text = self::esc($text);
    	if(!$text) throw new Exception("Wrong update text!");
    
    	mysql_query("	UPDATE tabel
    					SET text='".$text."'
    					WHERE id=".$id
    				);
    		
    	if(mysql_affected_rows($GLOBALS['link'])!=1)
    		throw new Exception("Couldn't update item!");
    }

    This worked before we changed to the textarea
    Can you see why?


    /Morten

  • #9
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    I'm not sure exactly what you are trying to do

    Are you trying to swap a text box element for a textarea and what event (button click?) triggers the swap. And what if there is something entered in the text box when the swap is triggered.

    The reason I ask is because it might be just a few lines of plain javascript if the above is all you want to do.

  • #10
    New Coder
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hey Bullant,

    Thank you for your answer.

    It is a free script I have downloaded and trying to change to match my needs. Therefore I Didn't want it to be a <input type=text> but instead a <textarea>.

    devnull69 helped me change that, but now the script wont save what i type in the field.
    You can press a button called "edit" and it turns into a writeable textfield, and then press save and it should upload the changes to a Database.
    It shows the changes like it should, but does not upload to the database anymore.


    I hope this helped you understand what the script does.

    Br,
    Morten

  • #11
    New Coder
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I found the error in the tabel name So know it works!

    Thanks alot for all your help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •