0

Problem statement: how to return a modified parameter value while returning its modified status in javascript function

sample code:

var inputHtml = 'hello';

if(IsContentChanged(inputHtml))
    alert(inputHtml);
else
    alert('No content changed');

function IsContentChanged(inputHtml)
{
    if($.trim(inputHtml))
    {
        inputHtml = 'new text';
        return true;
    }
    else
        return false;    
}
1
  • where is the comparison and logic for changed input text? Commented Aug 30, 2013 at 12:29

3 Answers 3

1

From MDN JavaScript reference:

All types except objects define immutable values. Specifically, strings are immutable.

So you have to return an updated string, no in-place edit is permitted.

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

Comments

1

checkout this link..

You can pass object by reference and can use this methodology for your requirement.

Pass by reference

Comments

1

Do this:

var inputHtml = {text:'hello'};

if(IsContentChanged(inputHtml))
    alert(inputHtml.text);
else
    alert('No content changed');

function IsContentChanged(inputHtml)
{
    if($.trim(inputHtml.text))
    {
        inputHtml.text = 'new text';
        return true;
    }
    else
        return false;    
}

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.