How can I get the value of a div with id=x

Asked

Viewed 1,130 times

-1

Well, what I intend to do is this:

I have the index.html page, with javascript I intend to do a json that gets me the value of a div with id="x" of the test.html page.

How can I do that?

  • What have you tried and what result? Do you have some code to help?

  • I have nothing, I have no idea how to do it with you

  • Put the HTML snippet then! Are you looking to retrieve a data-value attribute from the DIV? That’s it?

1 answer

0

Friend, Your Question is a little Vague. However I made 4 examples that can help you

Follows Below.

$("#btn1").click(function () {
    var conteudoDiv1 = $("#div1").text(); // Pegar texto Com Jquery
	alert(conteudoDiv1);
});

$("#btn2").click(function () {
    var conteudoDiv2 = $("#div2").data("value"); // Pegar Atributo Com Jquery
	alert(conteudoDiv2);
});

$("#btn3").click(function () {
    var conteudoDiv3 = document.getElementById("div3").getAttribute('data-value') // Pegar Atributo Sem Jquery
	alert(conteudoDiv3);
});

$("#btn4").click(function () {
    var conteudoDiv4 = document.getElementById("div4").innerText; // Pegar texto Sem Jquery
	alert(conteudoDiv4);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id ="div1"> DIV 1- Valor da DIV </div>
  <div data-value='VALOR DATA VALUE' id ="div2" > DIV 2</div>
  <div data-value='VALOR DATA VALUE SEM JQUERY' id ="div3">DIV 3</div>
  <div id ="div4">DIV 4 - Valor da DIV sem JQUERY </div>
  
  <input id="btn1" type="button" value = "Pegar valor DIV 1"/> 
  <input id="btn2" type="button" value = "Pegar valor DIV 2"/> 
  <input id="btn3" type="button" value = "Pegar valor DIV 3"/> 
  <input id="btn4" type="button" value = "Pegar valor DIV 4"/>

Browser other questions tagged

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