0

I have a gender selector, and a script that gets the value of the selected gender and pastes it on a label, but it's pasting the value: "M" or "F".

<div name="userLabelDiv" id="genderUserLabelDiv">
    <label class="required">Sexo</label>    
    <label id="genderLabel">F</label> 
</div>

Now when I'm trying to get this letter and replace for the actual gender, I'm missing on the JS script. Could you help me?

$("#genderLabel").html(
    if ($(this).value == 'F') {
        this.value = 'Female';
    }
    if ($(this).value == 'M') {
        this.value = 'Male';
    }
);

I know the script is probably wrong, could someone correct it, please? Thank you!

4 Answers 4

3
var gender = $('#genderLabel');
if (gender.text() == 'F') { gender.text('Female'); }
else { gender.text('Male'); }
Sign up to request clarification or add additional context in comments.

1 Comment

This code, is giving me syntax errors: $("[name=userDto\\.gender]").change(function() { if ($(this).text() == 'F') { $(this).text('Femenino'); } else { $(this).text('Masculino'); } });
2

In jQuery 1.4+ you can pass a function to .text() like this:

$("#genderLabel").text(function(i, t) {
   return t == 'F' ? 'Female' : t == 'M' ? 'Male' : t;
});

This converts F to Female, M to Male and leaves anything else alone, in case new genders come up :)

2 Comments

SyntaxError: missing : in conditional expression { message="missing : in conditional expression", more...}
@BoDiE2003 - woops typo, fixed :)
2

replace every value with text()

if($(this).text()=='F'){
   $(this).text('Female');
}
if($(this).text()=='M'){
    $(this).text('Male');
}

Among a few other things

Comments

1

To use .html() that way, you need to pass a function as the argument that returns the value you want.

Try it out: http://jsfiddle.net/FDZBJ/

$("#genderLabel").html(function(i,html) {
                           html = $.trim(html);
                           if(html == 'F') return 'Female';
                           if(html == 'M') return 'Male';
                           return html;
                       });

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.