0

I've seen others do these things to create a dynamic table element or other HTML on a page:

var $myRow = $('#myRowContainer' + IDRow);
var $column = $('<div />', { 'class': 'col', 'css': { 'width': '30%' } }).appendTo($myRow);
$('<div />', { css: { 'text-align': 'left' } }).append($('<span />', { 'html': objDetails[0].message })).appendTo($column);`

I often create html stuff in javascript like:

 jQuery.each(mytable, function (i, obj) {
                    var newrow = ' <tr> ';
                    newrow += ' <td> ';
                    newrow += ' <span> ' + obj.ID + '</span>';
                    newrow += ' </td> ';
                    newrow += ' <td> ';
                    newrow += ' <span> ' + obj.PartNumber + ' </span>';
                    newrow += '</td>';
                    newrow += ' <td> ';
                    var name1 = 'txEI_';
                    var consecutive = obj.IDc;
                    var idLabelText = name1 + consecutive;
                    newrow += ' <input id="' + idLabelText + '" class="' + myCssClassToDetect + '" type="text" value="0" /> ';
                    newrow += ' </td> ';
                    newrow += ' </tr> ';
                    $("#tbDesglose").append(newrow);
                    //make spinner for new field
                    makeSpinner(idLabelText);
                });

What's the difference? Why choose one over another?

1 Answer 1

1

When I see var prefixed with $ in javascript, it just usually means the variable is storing some element that is already wrapped in jQuery.

It's just a coding convention some people do.

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

2 Comments

And it's smart to differentiate semantically between a DOM node and a jQuery object wrapping a DOM node.
Ok, thanks. I get more clear with the creation of DOM elements using the JQuery object that wraps inside a DOM element. but still don't get clear with the $ before the variable, I think I will need some clearer example

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.