Picking up content from div with equal classes

Asked

Viewed 4,079 times

5

How do I get the content inside p? I got it, but it’s listing all the contents and not just what’s clicked.

And another question, how do I call a function in the onclick when she’s inside of $(document).ready? In the example it doesn’t work, even if I do it in a file .js even '-'

Here follows the code on Jsfiddle

  • The purpose of using jquery is to eliminate that lot of javascript online in html, and have the flexibility of selectors as in CSS, prefer to do everything with jquery as in this fiddle.

2 answers

5


when u put $(Document). ready does not work because the javascript functions should stay out of it.

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

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

code http://jsfiddle.net/q5yuqnq6/8/

  • It got super simple and better rsrs. Now I understand why $Document.ready doesn’t work. Thanks, man. I will give a study on the use of Parent(), I had not seen anything with it ^^

  • Parent() does no more than refer to the parent of the element in question, for example, if you use the relative in a li it will reference the parent ul.

  • 1

    +1 only that I would go a little further: in the same way that you removed the insertDado of onclick (using a jQuery selector instead), I would do the same with the filtraServico. Thus, we do not "pollute the namespace" by adding new global functions without need. Example

4

You’re taking all the content because when you put it $('.insert').text() you take the text of all elements with class . Insert.

It works if you do a click event:

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

This way it takes the text from the place where you clicked.

To do the click function is this same way, and works normal within $(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 will use this within the function insertDado(), you have to perform this function after you set, just by writing insertDado() within the $(document).ready.

I hope you’re not too confused.

  • And your explanation was very clear, it was quiet to understand ^^

  • I’m on the phone but it feels right; but why did you comment on Document.ready? And you need to perform this function to work. Write her name inside Document.ready.

  • If you have seen the comment of me saying that it did not work, forget it. It did work. It is that it is not opening on onclick and yes when you click on the element li rsrs.

Browser other questions tagged

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