0

I am using jQuery to generate input elements for a dynamic form. For the most part, things are working out well, but I'm having a problem setting the text property of the elements... specifically, the text values don't render on the page, though they appear correct in the elements tab of the console.

For instance...

function generateInput(siteNumber, x){

    adult = $("<input/>", {
            type: 'radio',
            id: 'adult'+siteNumber+''+x+'',
            name: 'age'+siteNumber+'['+x+']',
            value: 'adult',
            text: 'Adult'
            });

    juvenile = $("<input/>", {
               type: 'radio',
               id: 'juvenile'+siteNumber+''+x+'',
               name: 'age'+siteNumber+'['+x+']',
               value: 'juvenile',
               text: 'Juvenile'
               });

    return adult.append(juvenile);

};

$(document).on("change", ".number_select", function(){
    siteNumber = $(this).parent().attr('data-site_number');
    numberFound = $(this).val();

    for(x = 1; x <= numberFound; x++){
        this['inputArray' + siteNumber].push(generateInput(siteNumber, x));
     };

     $(this).parent().append(this['inputArray' + siteNumber]);
});

Despite the fact that the console displays

<input type="radio" id="adult11" name="age1[1]" value="adult">Adult</input>
<input type="radio" id="juvenile11" name="age1[1]" value="juvenile">Juvenile</input>

these text elements do not appear on the page - the two radio buttons are there, no text. I would appreciate it if someone could help me understand why this is happening and what I can do to fix it. Thanks very much!

EDITING To adeneo's point, input tags are self-closing, so no dice with .text(). However, I have also tried .prepend()/.append() and similarly, nothing doing...

adult = $("<input/>", {
     type: 'radio',
     id: 'adult'+siteNumber+''+x+'',
     name: 'age'+siteNumber+'['+x+']',
     value: 'adult'
     }).prepend('Adult');

juvenile = $("<input/>", {
     type: 'radio',
     id: 'juvenile'+siteNumber+''+x+'',
     name: 'age'+siteNumber+'['+x+']',
     value: 'juvenile'
     }).prepend('Juvenile');
5
  • 2
    An input is a self closing element, it has no text, only a value, nor does it have a closing tag. Commented Apr 1, 2016 at 20:42
  • Good call - also tried .prepend() to no avail - editing question to reflect this. Commented Apr 1, 2016 at 20:46
  • As it's a self closing element, you can't prepend or append to it, it can't have children. Commented Apr 1, 2016 at 20:49
  • You'll have to add the text to the parent element, for instance wrapping both the input and the text in a span etc. Commented Apr 1, 2016 at 20:49
  • 1
    Here's how BTW -> jsfiddle.net/gczg61vq/1 Commented Apr 1, 2016 at 21:09

1 Answer 1

1

I would create a label with the text you want, then prepend the input to the label element:

adult = $("<label>Adult</label>").prepend($("<input/>", {
        type: 'radio',
        id: 'adult'+siteNumber+''+x+'',
        name: 'age'+siteNumber+'['+x+']',
        value: 'adult'});
Sign up to request clarification or add additional context in comments.

3 Comments

As a best practice, you should also add the 'adult'+siteNumber+''+x+'' generated id tag as a for attribute on the label.
Thank you - for whatever reason, I had to parse it like this - adult = $("<input/>", { type: 'radio', id: 'adult'+siteNumber+''+x+'', name: 'age'+siteNumber+'['+x+']', value: 'adult' }); adultLabel = $("<label/>", { text: 'Adult', for: 'adult'+siteNumber+''+x+'' }).append(adult);
Using the syntax that you had prepended the label with an empty text input field. Any idea why?

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.