Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8880249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:07:47+00:00 2026-06-14T20:07:47+00:00

I have some element with some data. I need to create changing field for

  • 0

I have some element with some data. I need to create changing field for it. So i use textarea to change text in this element. Everything working nice but when i want to add condtion with text langht something is not working right… This is my code and some fiddle (http://jsfiddle.net/qzzKA/) for it. Thx for help…

<div class="textchange_box">
    <span class="desc">data<br>data
    </span>
    <textarea name="desc" class="textfield"></textarea>
    <span class="change">change</span>
    <span class="save">save</span>
</div>​ 

and jquery:

$(".change").live('click', function () {
    $(this).prev('.textfield').css('display','block');
    $(this).prevAll('.desc').css('display','none');
    $(this).next('.save').css('display','block');
    $(this).css('display','none');
});
$(".save").live('click', function () {
    var default_value = $(this).prev('.desc').text().replace(/\r\n|\r|\n/g,"<br/>");
    var actual_value = $(this).prev('.textfield').text().replace(/\r\n|\r|\n/g,"<br />");
    $(this).prevAll('.textfield').css('display','none');
    $(this).prev('.change').css('display','block');
    $(this).css('display','none');
    if (actual_value.length < 0) {
        $(this).prev('.desc').replaceWith('<span class="desc">' + default_value +'</span>');
    }
    else if (actual_value.length > 0) {
        $(this).prev('.desc').replaceWith('<span class="desc">' + actual_value +'</span>');
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:07:49+00:00Added an answer on June 14, 2026 at 8:07 pm

    Replace your following line:

    if (actual_value.length < 0) {
    

    for this one:

    if (actual_value.length <= 0) {
    

    because when a string is empty its length is zero not a negative number.

    UPDATE:
    You have other errors, your following two lines:

    var default_value = $(this).prev('.desc').text().replace(/\r\n|\r|\n/g,"<br/>");
    var actual_value = $(this).prev('.textfield').text().replace(/\r\n|\r|\n/g,"<br />");
    

    should be like this:

    var default_value = $(this).prevAll('.desc').text().replace(/\r\n|\r|\n/g,"<br/>");
    var actual_value = $(this).prevAll('.textfield').val().replace(/\r\n|\r|\n/g,"<br />");
    

    So using prevAll() instead of prev() and using val() for textarea instead of text() .

    Also you need to use prevAll() here:

    $(this).prev('.desc').replaceWith('<span class="desc">' + actual_value +'</span>');
    

    so it would be:

    $(this).prevAll('.desc').replaceWith('<span class="desc">' + actual_value +'</span>');
    

    See working fiddle .

    Basically the most common error is you thought .prev() will traverse all previous siblings, while for that you have to use prevAll()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data in an XML element that looks like this: <?xml version=1.0
I have some particular need to clone an element (image) and be draggable inside
I have HTML page with some HTML element with ID=logo . I need to
I have a div element containing some text. When the user clicks a word
I have a td-element which can contain some HTML code, e.g. &lt; . This
Say I have the following element: <p id=thingy>Here is some <em>emphasized</em> text!</p> In a
I have to dynamically create series of links that when clicked - change some
i have some element in my container and want to save all properties of
I have a via with some jQuery function to get value from HTML element
I need to create module in Magento which will have few database tables. One

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.