0

This is my original code to create an input field:

$('#dynamicForm').append('<li id="pdport"></li>');
$('#pdport').append("<label for='sport'>Destination port</label>")
$('<input>').attr({type: 'text', id: 'dport', name: 'dport'}).appendTo('#pdport');
$('#pdport').append("<span>Enter the destination port here (0-65535)</span>");

Instead of doing this code over and over I'm trying to create a function for it

function createInputfield(mainID, childID, labelFor, labelText, spanText){
    $(mainID).append('<li id=childID></li>');
    $(childID).append("<label for=labelFor>labelText</label>")
    $('<input>').attr({type: 'text', id: labelFor, name: labelFor}).appendTo(childID);
    $(childID).append("<span>spanText</span>");
}

and than calling to function with

createInputfield("#dynamicForm", "#psport", "sport", "Source port", "Enter the source port here (0-65535)");

this way I can create a lot of dynamic input fields without copy/pasting a lot of the same code.

The problem is that this doesn't work, I always get syntax errors, like you can't append on mainID and others. I tried a lot of different ways to accomplish this like with quotes, with single quotes, without.. But nothing seems to work.

3 Answers 3

2

You forgot to scape your variables and concatate they to the strings

function createInputfield(mainID, childID, labelFor, labelText, spanText){
    $(mainID).append('<li id='+childID.replace('#','')+'></li>');
    $(childID).append("<label for="+labelFor+">"+labelText+"</label>")
    $('<input>').attr({
        type: 'text', 
        id: labelFor, 
        name: labelFor
    }).appendTo(childID);
    $(childID).append("<span>"+ spanText +"</span>");
}

This might works

Sign up to request clarification or add additional context in comments.

Comments

1

Marvin is close. He caught the lack of string concatenation. You also forgot that you are sending in string that begin with a # but are using those strings in place that can not accept a string that begins with a #.

Update: Looks like Marvin caught the usage of # as well. He opted to strip it. My solution adds the hash where needed, so you don't have to worry about passing the hash into the function.

JS:

function createInputfield(mainID, childID, labelFor, labelText, spanText){
    $('#'+ mainID).append('<li id=' + childID + '></li>');
    $('#'+ childID).append('<label for=' + labelFor + '>' + labelText + '</label>')
    $('<input>').attr({type: 'text', id: labelFor, name: labelFor}).appendTo('#'+ childID);
    $('#'+ childID).append('<span>' + spanText + '</span>');
}

createInputfield("dynamicForm", "psport", "sport", "Source port", "Enter the source port here (0-65535)");

Demo: https://jsfiddle.net/hopkins_matt/6xyjawgs/

1 Comment

his sending the id selector in the string, i just removed the hash with .replace method
0

taking a momment, this is my code, expect like it, i concat object in array and then put all together

function createInputfield(mainID, childID, labelFor, labelText, spanText){


    var elms = [
        $('<label for=' + labelFor + ' />').text(labelText),
        $('<input />').attr({type: 'text', id: labelFor, name: labelFor}),
        $('<span />').text(spanText)
    ];
    $('<li id=' + childID + '></li>').append(elms).appendTo('#'+ mainID)

}

createInputfield("dynamicForm", "psport", "sport", "Source port", "Enter the source port here (0-65535)");

hope you like it

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.