0

I have a button that does the following to rows that are checked. I added .join('') to remove a comma that will appear from 2nd return value onwards

var getName = $("input:checkbox:checked").map(function() {
return ($(this).parent().next().next().next().next().next().next().text());
}).get().join('');

var getNum = $("input:checkbox:checked").map(function() {
return ($(this).parent().next().next().next().next().text());
}).get()join('');

alert(getName.concat(getNum));   

The output is as follows when i checked 3 rows and clicked the button:

Name1
Name2
Name3
Num1

Num2

Num3


When i stringify the return elements:

var getName = $("input:checkbox:checked").map(function() {
return JSON.stringify($(this).parent().next().next().next().next().next().next().text());
}).get().join('');

var getNum = $("input:checkbox:checked").map(function() {
return JSON.stringify($(this).parent().next().next().next().next().text());
}).get()join('');

alert(getName.concat(getNum));    

The output is as follows:

"\n Name1""\n Name2""\n Name3""\n Num1\n ""\n Num2\n ""\n Num3\n "


How can i get the output to be like this?

Name1 Num 1
Name3 Num 2
Name3 Num 3

1 Answer 1

0

You can try join the two map call to one, like this:

var getName = $("input:checkbox:checked").map(function() {
return $(this).parent().next().next().next().next().next().next().text()
    + ' ' + $(this).parent().next().next().next().next().text();
}).get().join('');

alert(getName);

by the way, why are you using so much "next()", what about mark the target DOM element with a "id" or "class" attributes?? Cause now it looks a little scary >_<|||

Hope this can help

5
  • Thanks. it works. Now it shows Name1</n>Num1</n>Name2</n>Num2. Any idea how to remove </n> between Name & Num? Apr 24, 2016 at 6:48
  • So if I name the first id as gName and the 2nd as gNum how do I mark the target DOM element? Apr 24, 2016 at 7:00
  • 1. remove the "</n>" use the "replace"function, eg: $(this).parent().text().replace('</n>', ''), 2. select the DOM element use id selector, like "#id", in your case, you want select the sibling after '$(this).parent()' whose id attributes is 'gName', try $(this).parent().nextUntil('#gName')
    – baiyangcao
    Apr 24, 2016 at 8:05
  • Worked perfectly! Cheers! Apr 24, 2016 at 9:06
  • glad to hear that, best wish! ^_^
    – baiyangcao
    Apr 24, 2016 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.