How can I catch and hold on to this value

Asked

Viewed 46 times

3

I have a program too big for here and so I created a fiddle to illustrate my problem.

I want to catch and store in a variable p, but the alert is always sending empty. Can help me sff?

HTML

<ul>
    <li>1</li>
    <li>2</li>
    <li class = "middle_item">
        <a>
            <div id= "box">
                <div id= "header">
                    <p>1234567</p>
                </div>
            </div>
            <div></div>
            <div></div>

        </a>           
    </li>
    <li>4</li>
    <li>5</li>
</ul>

Javascript

var aqui = $("li .middle_item .header").text();
alert(aqui);

FIDDLE

1 answer

2


You must bear in mind that:

  • must be without space here: li .middle_item for it is the very element li classy middle_item and not descended from.

  • the # is the ID selector, you must then change the class selector . for #.

Note also that since Ids are unique then the part li.middle_item is redundant.

Must stay then:

var aqui = $("#header p").text();

jsFiddle: http://jsfiddle.net/v0ss1mkt/

  • That is not the fiddle. the correct fiddle http://jsfiddle.net/ufkoqeu8/6/

  • @msm.oliveira ah, you changed the question...

  • @msm.oliveira so this is what you want: http://jsfiddle.net/ufkoqeu8/7/

  • 1

    what a stupid mistake on my part . I got it. I was using . instead of # . That div has id and no class

  • @msm.oliveira happens to everyone, nice to have another pair of eyes to see sometimes :) Remember that it should be without space here: li .middle_item and that the # is the ID selector.

  • 1

    ok. thanks. if you want to update the answer I accept

  • @msm.oliveira updated the reply and I will delete these comments here later too.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.