jg100309
jg100309

Reputation: 411

writing functions in JavaScript

Okay, so I'm still learning here.

Created a jsfiddle http://jsfiddle.net/JustJill54/GDVt6/ that will toggle visibility on name and email fields based on Y/N anonymous drop down. I wrote a function to clear the contents of the name and email fields if they have existing values.

To test it, I put a default value into the name, but the value of the name field is not being reset to an empty string as I specified in my function. Can anyone tell me why the name input is not being blanked out?

Many Thanks!

JavaScript:

function clearID ()
{
    $("input[title='name']").val() == '';
    $("input[title='email']").val() == '';
}

if ($("select[title='anonymous']option:selected").val() !== "Yes") {
    $(".anon").toggle();

    $("select[title='anonymous']").change(function() {
        //$(".anon").toggle();
        clearID();
        $(".anon").toggle();
    }); //close anonymous.change
}

Upvotes: 0

Views: 124

Answers (3)

tsilik_
tsilik_

Reputation: 94

$("input[title='name']").val("");

the same for the email

Upvotes: 0

Andrew
Andrew

Reputation: 13853

From the val() documentation,

The correct way to set a value is,

$("input[title='name']").val('');

http://jsfiddle.net/GDVt6/5/

Upvotes: 2

Logan Serman
Logan Serman

Reputation: 29870

Change your function to:

function clearID()
{
    $("input[title='name']").val('');
    $("input[title='email']").val('');
}

Upvotes: 1

Related Questions