0

I want to take the text of first row from body in wordpress and I have some problems.

My code: HTML CODE

<body id='asd'>
   <h2>Text1</h2>
   <p>Text2</p>
   <b>Text3</b>
</body>

JS CODE:

console.log(jQuery('#asd').children(':first').text());

This code is working locally. It will output in console, exactly what I need: Text1, but, in wordpress doesn't work.

If I try the same code on wordpress, no output is show.

If i try:

console.log(jQuery('#asd').children(':first'));

On local output is: [h2, prevObject: r.fn.init(1)] On wordpress output is: [selector: "", prevObject: a.fn.init, context: document]

Can someone help?

Kiddo
  • 913
  • 2
  • 9
  • 13
  • According to your output mentioned above the element is not present on the DOM. Can you provide some more details on the how is the html structure on your wordpress site. – Harshit Jain May 08 '17 at 11:13
  • @HarshitJain I don't understand your question.. – Kiddo May 08 '17 at 11:18
  • has wordpress removed the id from your body? why not just try `$('body').children().eq(0)`. Is that the fullpage in wordpress? - ie you are not trying to use multiple body tags? – Pete May 08 '17 at 11:40
  • @Pete There are multiple body tags... And that id isn't removed, it's still there. – Kiddo May 08 '17 at 12:25
  • html documents can only have one body tag - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body, perhaps you should use an article tag instead – Pete May 08 '17 at 12:26
  • It's wordpress.. i can't change the 2nd body. Is already made by wordpress itself – Kiddo May 08 '17 at 12:28
  • Well you have to change something as your code is invalid and that's why it's not working – Pete May 08 '17 at 12:29
  • And its something like that: ` ` – Kiddo May 08 '17 at 12:29
  • @Kiddo: The element `#asd` is present on your local but not on the wordpress site and hence the `[selector: "", prevObject: a.fn.init, context: document]` output. This output states that the element `#asd` is not present. Can you please check in the browser console by searching for `asd`. – Harshit Jain May 08 '17 at 12:31
  • You had right. Can you get somehow the first element from the second body? Or it's not possible? – Kiddo May 08 '17 at 12:34
  • 1
    if it is in an iframe then it is okay - to get content out of an iframe, you need to target the frame - http://stackoverflow.com/questions/1796619/how-to-access-the-content-of-an-iframe-with-jquery – Pete May 08 '17 at 12:54
  • It's working ! Thank you a lot, @Pete – Kiddo May 08 '17 at 12:56
  • Thank you too, @HarshitJain – Kiddo May 08 '17 at 12:57
  • Can you put a new answer, with what you said, to close this topic? Thanks. – Kiddo May 08 '17 at 13:05

3 Answers3

1

From your comments, it looks as if the body is in a separate iframe that is within the main document. To target an iframe's contents you need to do the following:

$('#iframe-id').contents().find('#asd').children(':first').text()
Pete
  • 57,112
  • 28
  • 117
  • 166
0

Just use the following

jQuery(document).ready(function(){
    jQuery('#asd:first-child').text();
}

It will return the first children tag value.

Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33
0

I think you need to put code in jQuery document ready event. Your code may execute before body tag loaded in DOM.

Malay Solanki
  • 80
  • 2
  • 10