Get div content with equal classes

5

How do I get content that is within p ? I did it, but it's listing all the content and not just what's clicked.

And another question, how do I call a function in onclick when it is inside $(document).ready ? In the example it does not work, nor do I do it in a file .js same '-'

Here's the code in JSFiddle

    
asked by anonymous 19.08.2014 / 04:06

2 answers

5

When you put $ (document) .ready does not work because the javascript functions should be outside of it.

I made some changes to the javascript, take a look and see if you understand. I removed the insertDate () function by:

$('#listaServicos li a').click(function() {
    var dado = $(this).parent().find('p').text();
    $('#dado').val(dado);
    alert(dado);
});

code link

    
19.08.2014 / 04:34
4

You're getting all the content because when you put $('.insert').text() you get the text of all elements with .insert class.

It works if you do a click event:

$(".insert").click(function(){
    var conteudo = $(this).text();
});

That way it gets the text where you clicked.

To do the click function it's just the same, and it works normal inside $ (document) .ready. The syntax is

$("seletor").click(function(){
  // o que acontece ao clicar
});

As you may have noticed with the first example I gave, hehe. If you are going to use this within the insertDado() function, you have to execute this function after setting, just writing insertDado() within $(document).ready .

I hope you did not get too confused.

    
19.08.2014 / 04:20