tags:

views:

25

answers:

1

Hi,

I'm trying to check whether val() has a specific value or not inside a form, if so deleting the contents / preventing the form being submitted depending on if both fields are filled in (stringOne and stringTwo). There's a default text for each input field, the first being "Namn, telefonnr, sökord" and the second "Område, plats, ort". If a user only fills in the first field the second must be cleared before the form string is passed on and vice versa. Like so -

// "This" refers to the form's submit button
if (($(this).siblings('input.stringOne').val("Namn, telefonnr, sökord")) && ($(this).siblings('input.stringTwo').val("Område, plats, ort"))) {
    return false; // If nothing's filled in, then do not submit
} else {
    // If either field is not filled in, clear it
    if ($(this).siblings('input.stringOne').val("Namn, telefonnr, sökord")) {
        $(this).siblings('input.stringOne').val() == '';
    }
    if ($(this).siblings('input.stringTwo').val("Område, plats, ort")) {
        $(this).siblings('input.stringTwo').val() == '';
    }
}

jQuery version 1.2.6.

+2  A: 

in jQuery, when you place a value inside val(), you are setting the value.

$('myElement').val()  // returns the value

$('myElement').val('some string')  // sets the value

.val() - http://api.jquery.com/val/

var $strOne = $(this).siblings('input.stringOne');
var $strTwo = $(this).siblings('input.stringTwo');

// "This" refers to the form's submit button
if ( (!$strOne.val() || $strOne.val() == "Namn, telefonnr, sökord") && (!$strTwo.val() || $strTwo.val() == "Område, plats, ort" ) {
    return false; // If nothing's filled in, then do not submit
} else {
    // If either field is not filled in, clear it
    if ($strOne.val() == "Namn, telefonnr, sökord") {
        $strOne.val("");
    }
    if ($strTwo.val() == "Område, plats, ort" ) {
        $strTwo.val("");
    }
}
patrick dw
I was about to come to that conclusion :) Tried checking against val() == '' also but it gave no results.
Staffan Estberg
@Staffan - I'm a little confused by your use of `.siblings()`. Are there just 2 input fields, `.stringOne` and `.stringTwo`?
patrick dw
Patrick you're correct. Earlier I was referring to one single input field hence the use of siblings. I will remove them :)
Staffan Estberg
@Staffan - I didn't notice that `$(this)` referred to the submit button (even though you stated it). Makes sense now.
patrick dw
I tried your code and it doesn't work, remember there are already existing values in both fields (the default "Namn, telefonnr, sökord" and "Område, plats, ort").
Staffan Estberg
@Staffan - Ah, I see. I'll update.
patrick dw
@Staffan - The updated version tests if the `input` elements are empty, or if they have the default value.
patrick dw
It works! :) Thank you!Not sure I understand why though, when I compare to an earlier script I did that used val() == '' as a checker on the input fields instead of variable placeholders, the end result should be the same?
Staffan Estberg
Yes, the variable is not necessary. Just gives better performance. If you were doing `.val() == ""` as a test, it would work. In your question you were using `==` to assign the value inside the `if()` statement. And you were using `.val('someValue')` as the test, which actually assigns the value.
patrick dw