Reputation: 337
I have some code used for identifying which links are clicked in a drop down menu, and reading back the hierarchy. (Parent, Child, Grandchild). The code works great and I don't want to change this.
The problem is there are line breaks and carriage returns in the html, and it's including them in my alert, causing issues with the reporting.
Is there a way I can take this existing code, and add something that will ignore/strip out any breaks or carriage returns?
$("ul#menudropd .myclass li a").click(function() {
var granchild = $(this).text(),
parent = $(this).closest(".anotherclass").find("a:first").text();
if ($(this).hasClass("subbold")) {
child = "";
} else {
child = $(this).closest("li").prevAll("li:has(a.subbold)").first().find('a').text();
}
alert(parent + ":" + child + ":" + grandchild);
});
Some of the alerts come back like this: "ClickedParent ::
ClickedGrandChild"
I want: "ClickedParent : : ClickGrandChild" every time.
Upvotes: 1
Views: 267
Reputation: 35670
This will remove carriage return and line feed codes:
alert((parent + ":" + child + ":" + grandchild).replace(/(\n|\r)/g,''));
Upvotes: 1
Reputation: 7045
it is because child
is undefined or null. You need to check for child to be undefined or null and based on that you can show alert. Something like this,
if(child!=null && typeof child != 'undefined')
alert(parent + ":" + child + ":" + grandchild);
else
alert(parent + ":" + " " + ":" + grandchild);
Upvotes: 0