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

0

Well, what I want to do is do the following:

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

How can I do this?

    
asked by anonymous 23.03.2017 / 00:02

1 answer

0

My friend, your question is a bit vague. But I did 4 examples that can help you.

Follow 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><divid="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"/>
    
23.03.2017 / 02:51