As Steven Xu correctly mentioned, inserting HTML strings is faster than manipulating the DOM, which is why I'd recommend creating elements with string manipulation instead of jQuery.
You need only change this:
var s = "<a title=\"Blah\" href=\"javascript:BlahFunc('" + options.rowId +
"')\">This is blah<a>";
to this:
var s = "<a title=\"Blah\" href=\"javascript:BlahFunc('" + options.rowId +
"')\">This is blah</a>";
(close <a>
tag with </a>
at the end of the string).
The string manipulation is much faster than the DOM Manipulation (see this for example). Moreover the difference will be much more if you try to insert a DOM fragment in the middle of a large HTML code. The usage of DOM DocumentFragments can a little improve the performance, but the usage of the string concatenation is the fastest way.
All other answers wrote his answer without the knowledge about the context (jqGrid custom formatter) where you use it. I try to explain why it is important in your case.
Because of performance advantages, jqGrid builds HTML code fragments for the grid first as the array of strings, then build one string from the string array with respect of .join('')
and insert the result in the table body at the end of all only. (I suppose that you use gridview:true jqGrid option which is almost always recommended). The jqGrid custom formatter is a callback function used by jqGrid during the building of the grid (table) body. The custom formatter must return the HTML code fragment as the string as the result. The string will be concatenated with other strings which build the body of the grid (table).
So if you will change your current code from pure string manipulation to the jQuery DOM manipulation and converting the results to the string (which needed be returned as the result of the custom formatting) then your code will work slowly and you will have no other advantages*.
The only real disadvantage of the usage of the string manipulations is the problem of the verification of the code which you build. For example, the usage of code without the close tags </a>
is a potential problem which you can have. In the most cases the problem will be solved during the inserting of the DOM fragment, but sometimes you can receive real problems. So you should just test the code which you inserted very carefully.
One more remark: If you want to follow unobtrusive JavaScript style you can use "#" as the value for the href
attribute and bind the corresponding click
event inside of gridComplete
or loadComplete
event handler. If you will have problems with the implementation of this you can open a new question and I will try to write the corresponding code example for you.
Note: I think the best implementation way will be the usage of onCellSelect
or beforeSelectRow
instead of binding click
event to every <a>
element in the column. I recommend to read the following answers for details: this one, another one and one more old answer.