I've been unable to get access to really simple pieces of HTML using cheerio, I just finished opening up the object $(this)
object and tracking down exactly what I need. Now an error occurs when there is no object in the stack, like a sibling or child. What's the correct way to select the previous sibling, get it's name, href & content, as well as the parent's css.
var code = $('code.lang-javascript, code.lang-js')
dataSet.blocks = []
code.map(function (i, elm) {
var block = {}
block.prevSiblingTag = $(this).parent().prev()[0].children[0].name
block.prevSiblingHref = $(this).parent().prev()[0].children[0].attribs.href
block.prevSiblingContent = $(this).parent().prev()[0].children[0].text()
block.parentTag = $(this)[0].name
block.parentClass = $(this)[0].attribs.class
block.code = $(this).html()
dataSet.blocks.push(block)
})
I was doing this and it was just a mess.
var $parent = $($(this).parent().html())
var $prevSibling = $($(this).parents().prev())
var block = {}
block.parentClass = $parent.attr('class')
block.prevSibling = $prevSibling.html()
block.code = $(this).html()
I've been unable to get access to really simple pieces of HTML using cheerio, I just finished opening up the object $(this)
object and tracking down exactly what I need. Now an error occurs when there is no object in the stack, like a sibling or child. What's the correct way to select the previous sibling, get it's name, href & content, as well as the parent's css.
var code = $('code.lang-javascript, code.lang-js')
dataSet.blocks = []
code.map(function (i, elm) {
var block = {}
block.prevSiblingTag = $(this).parent().prev()[0].children[0].name
block.prevSiblingHref = $(this).parent().prev()[0].children[0].attribs.href
block.prevSiblingContent = $(this).parent().prev()[0].children[0].text()
block.parentTag = $(this)[0].name
block.parentClass = $(this)[0].attribs.class
block.code = $(this).html()
dataSet.blocks.push(block)
})
I was doing this and it was just a mess.
var $parent = $($(this).parent().html())
var $prevSibling = $($(this).parents().prev())
var block = {}
block.parentClass = $parent.attr('class')
block.prevSibling = $prevSibling.html()
block.code = $(this).html()
Share
Improve this question
asked Aug 17, 2015 at 6:17
ThomasReggiThomasReggi
59.7k97 gold badges259 silver badges459 bronze badges
1 Answer
Reset to default 3The problem is if there is no children for the previous sibling then $(this).parent().prev()[0]
will be null.
So a better to get the jQuery obeject reference to the target element then use it to get the attribute/property values like
var code = $('code.lang-javascript, code.lang-js')
dataSet.blocks = code.map(function (i, elm) {
var block = {}, $this = $(this),
target = $this.parent().prev().children().eq(0);
block.prevSiblingTag = target.attr('name');
block.prevSiblingHref = target.attr('href');
block.prevSiblingContent = target.text()
block.parentTag = this.name
block.parentClass = $this.attr('class')
block.code = $this.html()
return block;
}).get();
Also since you are using .map()
, it can return an array