Attribute Starts With Selector = Attribute Starts With Selector?











up vote
0
down vote

favorite












I am trying to troubleshoot a bug written from another developer, and I do not understand the way the Attribute starts with selector is being used.



Inside a dialog box there are input fields with uniquely set id's.



And then when the user submits, the function is attached like so,



$('#dialogform').dialog({
autoOpen: false,
height: 925,
width: 1025,
modal: true,
buttons: {
Submit: function () {
var JQuerycollection = $('#dialogform').data();
if (JQuerycollection.isClosed.val() == "False") {

JQuerycollection["msg"] = "Submit";
JQuerycollection[$('[id^="ETI"]').prop('id')] = $('[id^="ETI"]').val();


and then stringified and sent through ajax to the backend:



data: JSON.stringify({ JSONcollection: JQuerycollection, dlgMsg: "Submit" })


There are two input elements that start with "ETI" for its ID, ETI_1234 and ETI_1235. When I use the debugger in FireFox, I can see only the first one is being added as a property (This is the bug), "ETI_1234 = 17"



Referencing the JQuery API: https://api.jquery.com/attribute-starts-with-selector/



From the API, I understand the example and how the selector works when the right side is not using another Selector, but I do not understand how it is being used in the code I am troubleshooting.










share|improve this question
























  • "Where the right side is a string". It's all a string. You seem to be confused with the id having numbers in it. But it's all a string. Attributes are strings.
    – Taplar
    Nov 13 at 15:38










  • Oh right, I meant a single value on the right side not another selector. I will edit.
    – eaglei22
    Nov 13 at 15:41






  • 1




    $('[id^="ETI"]') will find all elements on the page that have an id that starts with that substring, so it can contain multiple elements, however prop() only returns a single value. So it will return the prop of the first element in it's result stack. If you want to get the prop value from each of the elements found, you will have to map them. Or loop over them and add them to the object one at a time.
    – Taplar
    Nov 13 at 15:41












  • So since both sides might have multiple values will they stay in sync when iterated over? I understand the left side will grab the id property for each element with an id that starts with "ETI" but the right side I am having more trouble understanding. What happens when multiple values are found on the right hand side?
    – eaglei22
    Nov 13 at 15:46















up vote
0
down vote

favorite












I am trying to troubleshoot a bug written from another developer, and I do not understand the way the Attribute starts with selector is being used.



Inside a dialog box there are input fields with uniquely set id's.



And then when the user submits, the function is attached like so,



$('#dialogform').dialog({
autoOpen: false,
height: 925,
width: 1025,
modal: true,
buttons: {
Submit: function () {
var JQuerycollection = $('#dialogform').data();
if (JQuerycollection.isClosed.val() == "False") {

JQuerycollection["msg"] = "Submit";
JQuerycollection[$('[id^="ETI"]').prop('id')] = $('[id^="ETI"]').val();


and then stringified and sent through ajax to the backend:



data: JSON.stringify({ JSONcollection: JQuerycollection, dlgMsg: "Submit" })


There are two input elements that start with "ETI" for its ID, ETI_1234 and ETI_1235. When I use the debugger in FireFox, I can see only the first one is being added as a property (This is the bug), "ETI_1234 = 17"



Referencing the JQuery API: https://api.jquery.com/attribute-starts-with-selector/



From the API, I understand the example and how the selector works when the right side is not using another Selector, but I do not understand how it is being used in the code I am troubleshooting.










share|improve this question
























  • "Where the right side is a string". It's all a string. You seem to be confused with the id having numbers in it. But it's all a string. Attributes are strings.
    – Taplar
    Nov 13 at 15:38










  • Oh right, I meant a single value on the right side not another selector. I will edit.
    – eaglei22
    Nov 13 at 15:41






  • 1




    $('[id^="ETI"]') will find all elements on the page that have an id that starts with that substring, so it can contain multiple elements, however prop() only returns a single value. So it will return the prop of the first element in it's result stack. If you want to get the prop value from each of the elements found, you will have to map them. Or loop over them and add them to the object one at a time.
    – Taplar
    Nov 13 at 15:41












  • So since both sides might have multiple values will they stay in sync when iterated over? I understand the left side will grab the id property for each element with an id that starts with "ETI" but the right side I am having more trouble understanding. What happens when multiple values are found on the right hand side?
    – eaglei22
    Nov 13 at 15:46













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to troubleshoot a bug written from another developer, and I do not understand the way the Attribute starts with selector is being used.



Inside a dialog box there are input fields with uniquely set id's.



And then when the user submits, the function is attached like so,



$('#dialogform').dialog({
autoOpen: false,
height: 925,
width: 1025,
modal: true,
buttons: {
Submit: function () {
var JQuerycollection = $('#dialogform').data();
if (JQuerycollection.isClosed.val() == "False") {

JQuerycollection["msg"] = "Submit";
JQuerycollection[$('[id^="ETI"]').prop('id')] = $('[id^="ETI"]').val();


and then stringified and sent through ajax to the backend:



data: JSON.stringify({ JSONcollection: JQuerycollection, dlgMsg: "Submit" })


There are two input elements that start with "ETI" for its ID, ETI_1234 and ETI_1235. When I use the debugger in FireFox, I can see only the first one is being added as a property (This is the bug), "ETI_1234 = 17"



Referencing the JQuery API: https://api.jquery.com/attribute-starts-with-selector/



From the API, I understand the example and how the selector works when the right side is not using another Selector, but I do not understand how it is being used in the code I am troubleshooting.










share|improve this question















I am trying to troubleshoot a bug written from another developer, and I do not understand the way the Attribute starts with selector is being used.



Inside a dialog box there are input fields with uniquely set id's.



And then when the user submits, the function is attached like so,



$('#dialogform').dialog({
autoOpen: false,
height: 925,
width: 1025,
modal: true,
buttons: {
Submit: function () {
var JQuerycollection = $('#dialogform').data();
if (JQuerycollection.isClosed.val() == "False") {

JQuerycollection["msg"] = "Submit";
JQuerycollection[$('[id^="ETI"]').prop('id')] = $('[id^="ETI"]').val();


and then stringified and sent through ajax to the backend:



data: JSON.stringify({ JSONcollection: JQuerycollection, dlgMsg: "Submit" })


There are two input elements that start with "ETI" for its ID, ETI_1234 and ETI_1235. When I use the debugger in FireFox, I can see only the first one is being added as a property (This is the bug), "ETI_1234 = 17"



Referencing the JQuery API: https://api.jquery.com/attribute-starts-with-selector/



From the API, I understand the example and how the selector works when the right side is not using another Selector, but I do not understand how it is being used in the code I am troubleshooting.







jquery html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 15:41

























asked Nov 13 at 15:33









eaglei22

1,0711330




1,0711330












  • "Where the right side is a string". It's all a string. You seem to be confused with the id having numbers in it. But it's all a string. Attributes are strings.
    – Taplar
    Nov 13 at 15:38










  • Oh right, I meant a single value on the right side not another selector. I will edit.
    – eaglei22
    Nov 13 at 15:41






  • 1




    $('[id^="ETI"]') will find all elements on the page that have an id that starts with that substring, so it can contain multiple elements, however prop() only returns a single value. So it will return the prop of the first element in it's result stack. If you want to get the prop value from each of the elements found, you will have to map them. Or loop over them and add them to the object one at a time.
    – Taplar
    Nov 13 at 15:41












  • So since both sides might have multiple values will they stay in sync when iterated over? I understand the left side will grab the id property for each element with an id that starts with "ETI" but the right side I am having more trouble understanding. What happens when multiple values are found on the right hand side?
    – eaglei22
    Nov 13 at 15:46


















  • "Where the right side is a string". It's all a string. You seem to be confused with the id having numbers in it. But it's all a string. Attributes are strings.
    – Taplar
    Nov 13 at 15:38










  • Oh right, I meant a single value on the right side not another selector. I will edit.
    – eaglei22
    Nov 13 at 15:41






  • 1




    $('[id^="ETI"]') will find all elements on the page that have an id that starts with that substring, so it can contain multiple elements, however prop() only returns a single value. So it will return the prop of the first element in it's result stack. If you want to get the prop value from each of the elements found, you will have to map them. Or loop over them and add them to the object one at a time.
    – Taplar
    Nov 13 at 15:41












  • So since both sides might have multiple values will they stay in sync when iterated over? I understand the left side will grab the id property for each element with an id that starts with "ETI" but the right side I am having more trouble understanding. What happens when multiple values are found on the right hand side?
    – eaglei22
    Nov 13 at 15:46
















"Where the right side is a string". It's all a string. You seem to be confused with the id having numbers in it. But it's all a string. Attributes are strings.
– Taplar
Nov 13 at 15:38




"Where the right side is a string". It's all a string. You seem to be confused with the id having numbers in it. But it's all a string. Attributes are strings.
– Taplar
Nov 13 at 15:38












Oh right, I meant a single value on the right side not another selector. I will edit.
– eaglei22
Nov 13 at 15:41




Oh right, I meant a single value on the right side not another selector. I will edit.
– eaglei22
Nov 13 at 15:41




1




1




$('[id^="ETI"]') will find all elements on the page that have an id that starts with that substring, so it can contain multiple elements, however prop() only returns a single value. So it will return the prop of the first element in it's result stack. If you want to get the prop value from each of the elements found, you will have to map them. Or loop over them and add them to the object one at a time.
– Taplar
Nov 13 at 15:41






$('[id^="ETI"]') will find all elements on the page that have an id that starts with that substring, so it can contain multiple elements, however prop() only returns a single value. So it will return the prop of the first element in it's result stack. If you want to get the prop value from each of the elements found, you will have to map them. Or loop over them and add them to the object one at a time.
– Taplar
Nov 13 at 15:41














So since both sides might have multiple values will they stay in sync when iterated over? I understand the left side will grab the id property for each element with an id that starts with "ETI" but the right side I am having more trouble understanding. What happens when multiple values are found on the right hand side?
– eaglei22
Nov 13 at 15:46




So since both sides might have multiple values will they stay in sync when iterated over? I understand the left side will grab the id property for each element with an id that starts with "ETI" but the right side I am having more trouble understanding. What happens when multiple values are found on the right hand side?
– eaglei22
Nov 13 at 15:46












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










If you want to add all the elements to the object, you need to change:



JQuerycollection[$('[id^="ETI"]').prop('id')] = $('[id^="ETI"]').val();


to:



$('[id^="ETI"]').each(function(){
JQuerycollection[this.id] = this.value;
});


$('[id^="ETI"]').prop('id') will return only a single value as prop() only returns a single value. If the result stack of the selector has multiple elements, it will get only the value of the first result, not all of them.






share|improve this answer





















  • Okay, that makes more sense. I was thinking the left side Selector would iterate through each property found like in the example of the API.
    – eaglei22
    Nov 13 at 15:50


















up vote
1
down vote













The bug here is that $('[id^="ETI"]') is a selector that might return more than one item, but when used with methods like .prop() or .val() will only return the value of the first item of the multiple items.



To fix this, you should iterate through the possible multiple elements $('[id^="ETI"]') and then on each to do the value assignment.



Something like:



$.each($('[id^="ETI"]'), function(i, el) {
JQuerycollection[$(el).attr('id')] = $(el).val();
});





share|improve this answer

















  • 1




    Don't use the $.each() version of each when dealing with the result of a jQuery selector. Use the $().each version.
    – Taplar
    Nov 13 at 15:52











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284393%2fattribute-starts-with-selector-attribute-starts-with-selector%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










If you want to add all the elements to the object, you need to change:



JQuerycollection[$('[id^="ETI"]').prop('id')] = $('[id^="ETI"]').val();


to:



$('[id^="ETI"]').each(function(){
JQuerycollection[this.id] = this.value;
});


$('[id^="ETI"]').prop('id') will return only a single value as prop() only returns a single value. If the result stack of the selector has multiple elements, it will get only the value of the first result, not all of them.






share|improve this answer





















  • Okay, that makes more sense. I was thinking the left side Selector would iterate through each property found like in the example of the API.
    – eaglei22
    Nov 13 at 15:50















up vote
1
down vote



accepted










If you want to add all the elements to the object, you need to change:



JQuerycollection[$('[id^="ETI"]').prop('id')] = $('[id^="ETI"]').val();


to:



$('[id^="ETI"]').each(function(){
JQuerycollection[this.id] = this.value;
});


$('[id^="ETI"]').prop('id') will return only a single value as prop() only returns a single value. If the result stack of the selector has multiple elements, it will get only the value of the first result, not all of them.






share|improve this answer





















  • Okay, that makes more sense. I was thinking the left side Selector would iterate through each property found like in the example of the API.
    – eaglei22
    Nov 13 at 15:50













up vote
1
down vote



accepted







up vote
1
down vote



accepted






If you want to add all the elements to the object, you need to change:



JQuerycollection[$('[id^="ETI"]').prop('id')] = $('[id^="ETI"]').val();


to:



$('[id^="ETI"]').each(function(){
JQuerycollection[this.id] = this.value;
});


$('[id^="ETI"]').prop('id') will return only a single value as prop() only returns a single value. If the result stack of the selector has multiple elements, it will get only the value of the first result, not all of them.






share|improve this answer












If you want to add all the elements to the object, you need to change:



JQuerycollection[$('[id^="ETI"]').prop('id')] = $('[id^="ETI"]').val();


to:



$('[id^="ETI"]').each(function(){
JQuerycollection[this.id] = this.value;
});


$('[id^="ETI"]').prop('id') will return only a single value as prop() only returns a single value. If the result stack of the selector has multiple elements, it will get only the value of the first result, not all of them.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 at 15:45









Taplar

15k21529




15k21529












  • Okay, that makes more sense. I was thinking the left side Selector would iterate through each property found like in the example of the API.
    – eaglei22
    Nov 13 at 15:50


















  • Okay, that makes more sense. I was thinking the left side Selector would iterate through each property found like in the example of the API.
    – eaglei22
    Nov 13 at 15:50
















Okay, that makes more sense. I was thinking the left side Selector would iterate through each property found like in the example of the API.
– eaglei22
Nov 13 at 15:50




Okay, that makes more sense. I was thinking the left side Selector would iterate through each property found like in the example of the API.
– eaglei22
Nov 13 at 15:50












up vote
1
down vote













The bug here is that $('[id^="ETI"]') is a selector that might return more than one item, but when used with methods like .prop() or .val() will only return the value of the first item of the multiple items.



To fix this, you should iterate through the possible multiple elements $('[id^="ETI"]') and then on each to do the value assignment.



Something like:



$.each($('[id^="ETI"]'), function(i, el) {
JQuerycollection[$(el).attr('id')] = $(el).val();
});





share|improve this answer

















  • 1




    Don't use the $.each() version of each when dealing with the result of a jQuery selector. Use the $().each version.
    – Taplar
    Nov 13 at 15:52















up vote
1
down vote













The bug here is that $('[id^="ETI"]') is a selector that might return more than one item, but when used with methods like .prop() or .val() will only return the value of the first item of the multiple items.



To fix this, you should iterate through the possible multiple elements $('[id^="ETI"]') and then on each to do the value assignment.



Something like:



$.each($('[id^="ETI"]'), function(i, el) {
JQuerycollection[$(el).attr('id')] = $(el).val();
});





share|improve this answer

















  • 1




    Don't use the $.each() version of each when dealing with the result of a jQuery selector. Use the $().each version.
    – Taplar
    Nov 13 at 15:52













up vote
1
down vote










up vote
1
down vote









The bug here is that $('[id^="ETI"]') is a selector that might return more than one item, but when used with methods like .prop() or .val() will only return the value of the first item of the multiple items.



To fix this, you should iterate through the possible multiple elements $('[id^="ETI"]') and then on each to do the value assignment.



Something like:



$.each($('[id^="ETI"]'), function(i, el) {
JQuerycollection[$(el).attr('id')] = $(el).val();
});





share|improve this answer












The bug here is that $('[id^="ETI"]') is a selector that might return more than one item, but when used with methods like .prop() or .val() will only return the value of the first item of the multiple items.



To fix this, you should iterate through the possible multiple elements $('[id^="ETI"]') and then on each to do the value assignment.



Something like:



$.each($('[id^="ETI"]'), function(i, el) {
JQuerycollection[$(el).attr('id')] = $(el).val();
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 at 15:50









w3jimmy

532819




532819








  • 1




    Don't use the $.each() version of each when dealing with the result of a jQuery selector. Use the $().each version.
    – Taplar
    Nov 13 at 15:52














  • 1




    Don't use the $.each() version of each when dealing with the result of a jQuery selector. Use the $().each version.
    – Taplar
    Nov 13 at 15:52








1




1




Don't use the $.each() version of each when dealing with the result of a jQuery selector. Use the $().each version.
– Taplar
Nov 13 at 15:52




Don't use the $.each() version of each when dealing with the result of a jQuery selector. Use the $().each version.
– Taplar
Nov 13 at 15:52


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284393%2fattribute-starts-with-selector-attribute-starts-with-selector%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?