I have an script that when click a button do many things and also rename some elements, so at first I use first script and I see sometimes work, sometimes not, so change it to second script and all time worked perfectly, but there is a question. is this two scripts do same? what is the difference, rather than all of my script I just replace first one with second one.
This is first script
$(CloneTarget).find(':input[name="' + MainName + '"]').attr('name', NewSelectName);
And second one:
$(CloneTarget).find(':input').each(function () {
if ($(this).attr('name') == MainName) {
$(this).attr('name', NewSelectName);
}
});
Where is the problem with first one?
Edit
Also I use this:
$(CloneTarget).find(':input[id="' + MainId + '"]').attr('id', NewSelectId);
And every thing worked fine I am really confused here, I must mention MainName and MainId are so similar like: MainName = Model.Phones[0] and MainId = Model_Phones[0] is the difference is about . character or any other things?
Yes, there is a difference. Your second code snippet performs a string comparison between the element’s
nameattribute and the value ofMainName. The first one directly injects the value ofMainNameinto an Attribute Equals selector.Therefore, the first snippet will fail if
MainNamehappens to contain:',",[or\.