1

I have this when the page loads

<input class="input" id="accountNameUsernameForm_name" name="profile[real_name]" size="30" type="text" value="George">

When a submit trigger is pressed I want to check if the current text in the input field is the same as it's value attribute?

if ( $('.input').val() != $('.input').attr('value') ) {
        $(this).addClass('formLoading');
}

However the problem is, val() and the attr('value') are always the same. What function retrieves the current text that is in the input right now without looking at its value attribute?

thank you

btw. it doesn't work with text()!

0

6 Answers 6

7
// save the default value on page load
var defaultUsername = $('.input').val();

// on submit, compare
if ( $('.input').val() != defaultUsername ) {
        $(this).addClass('formLoading');
}
Sign up to request clarification or add additional context in comments.

4 Comments

+1, I totally didn't spopt that as the issue. Also worth mentioning that $('.input') will return everything with the class input so you can't guarantee that the input you are looking is the one you want. use the following instead: $("#accountNameUsernameForm_name")
@James Wiseman: Totally agree, just gave a context answer based on his code.
Link Only Answers are not helpful on [main]. If you are going to link to a post, then you need to summarize it in your answer. That way, if the link ever dies, this answer will still be useful. See this answer for clarification.
@CodyGuldner Spamming much? I don't have a single link in my answer. What exactly are you trying to say?
0

use id of input instead input and then try this..

inputValue = $("#accountNameUsernameForm_name").val()

and then in your form handler try this..

inputText =  $("#accountNameUsernameForm_name").attr('value') 

    if(inputValue ! = inputText ){
        $(this).addClass('formLoading');

}

Comments

0
$('.input').val() is equal to  $('.input').attr('value') always.

I suggest you include a hiddenfield or save the value in a variable

See example: http://jsfiddle.net/r3KZu/6/

Comments

0

The problem is that the value attribute is also updated as the text is changed. On page load, you should store the initial value as a variable, and then check against that in the callback.

Comments

0

Have a crack at this:

$('.input').each(function() {
    if($(this).val() != this.defaultValue) {
        $(this).addClass('formLoading');
    }
});

Comments

0

Use the defaultValue property.

$('.input')[0].defaultValue

The defaultValue property sets or returns the default value of a text field, specified in the value attribute.

Sometimes people forget jQuery is just JavaScript, simple things should be done the easy way.

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.