Reputation: 639
$('#selectID').append($('<option></option>').val(val).html(text));
$('#selectID').append('<option value="'+val+'">'+text+'</option>');
Where val = value during loop and text = data. Which is the better way to append? What is the difference in performance between two if there is lot of data?
Upvotes: 0
Views: 65
Reputation: 5060
If just text and value, I think it would be better
$("#selectID").append(new Option("text", "value"));
If you need to setting id or something else to option
$("#selectID").append($(new Option("text", "value")).attr("id", "id123"));
Upvotes: 0
Reputation: 18578
In my view the later one
is better and alos you dont need to use \
char if you are using combintaion of '
and "
. you can directly so it as
$('#selectID').append('<option value="'+val+'">'+text+'</option>');
Upvotes: 1
Reputation: 298392
I prefer this method, as "better" for me is readability:
$('<option />').val(val).text(text).appendTo('#selectID');
It looks cleaner and doesn't look like lisp.
Upvotes: 0
Reputation: 8198
2nd way is naturally faster as you're not creating an object and executing jQuery methods on it.
I'd also argue that the 2nd way is more readable, although thats debatable.
Upvotes: 0