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.
jquery html
add a comment |
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.
jquery html
"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, howeverprop()
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
add a comment |
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.
jquery html
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
jquery html
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, howeverprop()
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
add a comment |
"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, howeverprop()
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
add a comment |
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.
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
add a comment |
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();
});
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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();
});
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
add a comment |
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();
});
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
add a comment |
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();
});
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();
});
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
"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, howeverprop()
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