<section id="123">
<div id="456">
some Data Here
</div>
</section>
I am a beginner to this and I need to copy data from section and div with their respective ids and use it some where else in my code. Please Help. Thanks for your time.
You can use
var htmlstring = $("#123").html();
.html() Function in jquery
Put your HTML in a php variable , and use it as you want
$div = '<section id="123">
<div id="456">
some Data Here
</div>
</section>';
Related
Tag hierarchy in a webpage :
<body>
<div id='header'>
<h2>.....</h2>
</div>
<div id='main'>
<h2>...</h2>
//Some other content
<h2>...</h2>
</div>
<div id='footer'>
<h2>.....</h2>
</div>
</body>
[PROBLEM : ] From the above hierarchy structure of a webpaege, I want to extract only the <h2> tags which are inside the <div id='main'>. Can someone please please help me out ?
What I have tried is.... using HTML DOM of php $h2Tags = $htmlDom->getElementsByTagName('h2');, but this gives me all the <h2> tag which are outside of main div as well. Please guide me to a solution.
I have updated this to PHP:
h2_tags below will get list of h2s in main div:
$div_m = $htmlDom->getElementById('main');
$h2_tags = $div_m->getElementsByTagName('h2');
This is JS:
var div_m = document.getElementById("main");
var h2_tag = div_m.getElementsByTagName('h2');
I'm trying to extract some data from HTML DIV using Simple HTML Dom but it's not working right.
When I try to extract data from links/imgs it works fine but I have only a problem with DIVs.
HTML code:
<div class="price-container">
<div class="price-calc-container" id="attributes-calc-price">
<div class="current-price-container">
7,99 EUR
</div>
</div>
</div>
I want to extarct the number inside this div
$html = file_get_html("https://www.test.de/");
$preise = $html->find('div.current-price-container');
print_r($preise);
Sometime I will get an empty array sometimes nothing.
You have to start iterating over the find() result or use the last parameter to specify an index
$html = file_get_html("https://www.led-centrum.de/LED-Leuchtmittel/led-strahler/LED-Strahler-GU10/4058075160071.html");
$preise = $html->find('div.current-price-container', 0);
print_r($preise);
You can use a simple Jquery command like this
Try this
<div class="price-container">
<div class="price-calc-container" id="attributes-calc-price">
<div class="current-price-container">
7,99 EUR
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
var x = $('.current-price-container').text();
console.log(x);
</script>
I have some html in my php page that I fetched from database and will be used many times in my webpage, so I want to put it into a php variable for later use.
My sample code is
<div class="info">
Some content from database here...
<div class="more">
Some more text...
</div>
</div>
How to store this html into php variable?
Please also tell me how to echo content of that variable?
I do agree with #Jon Stirling but You can do like below:
$htmlData = '<div class="info">
Some content from database here...
<div class="more">
Some more text...
</div>
</div>';
and you can print this
echo $htmlData;
I read this article - Get DIV content from external Website . I get source of website with file_get_contents() function and I have to extract from it content of two divs with same class name.
I have very similar problem, but with divs with same class name. E.g. I have code like that:
<div class="baaa">
Some conete
</div>
<div class="baaa">
Second Content
</div>
I want to get both content of both these divs. Solution accepted in article I linked support only one. My expected result is array like this:
$divs[0] = "Some conete"
$divs[1] = "Second Content"
Please give me advice what to do. I read about DOMDocument class, but have no idea how to use it.
i have used the simple html dom parser and your content can be extracted as
$html = file_get_html('your html file link');
$k=1;
foreach($html->find('div.baaa') as $e){
$divs[$k]=$e;
$k++;
}
echo $divs[1]."<br>";
echo $divs[2];
You could use XPath. XPath is a query language for XML. There are PHP functions that support Xpath.
For you the example could be:
File test.html:
<html>
<body>
<div class="baaa">
Some conete
</div>
<div class="baaa">
Second Content
</div>
</body>
</html>
The php code that extracts contents of divs with the class "baaa"
$xml = simplexml_load_file('test.html');
$data = $xml->xpath('//div[#class="baaa"]/text()');
foreach($data as $row) {
printf($row);
}
generates the following output:
Some conete
Second Content
Look for XPath tutorials if you need more complex searching or analyzing.
Try it with your data:
$file_contents = file_get_contents('http://address.com');
preg_match_all('/<div class=\"baaa\">(.*?)<\/div>/s',$file_contents,$matches);
print_r($matches);
BTW: Polska rządzi :)
<script type="text/javascript">
$(document).ready(function(){
$('.baaa').each(function(){
alert($(this).text());
});
});
</script>
<div class="baaa">
Some conete
</div>
<div class="baaa">
Second Content
</div>
Probably a very simple answer, but I cannot seem to find a working solution. I am creating links from a php search script and it generates links based on query. I have written a sample jQuery script to open a div based on a tag being clicked, but when I click link, nothing happen. I cannot see any errors in firebug and would appreciate somke help. Thank you.
UPDATE: Added html and changed mailLink from id to class.
jQuery
$("a").click(function(e) {
e.preventDefault();
$('.mailShow').fadeIn(1500).html('This is the mailShow div');
});
PHP
<?php
while ($row = mysql_fetch_assoc($rsd))
{?>
<div class="each_rec"><?php echo $row['name_usr'];?> <?php echo $row['idcode_usr'];?></div>
<?php
}
if($total==0){ echo '<div class="no-rec">No Record Found !</div>';}
?>
HTML
<div id="content">
<div class="search-background">
<label><img src="loader.gif" alt="" /></label>
</div>
<div id="sub_cont">
<div class="mailShow"></div>
</div>
</div>
Generated HTML from firebug
<div class="each_rec">Demo User DEMO</div>
In jquery you have .mailShow and in PHP you use id="mailLink". You should change .mailShow to #mailLink.
JSFiddle for testing.
JSFIDDLE
Try this code:
$(document).ready(function () {
$(document).on("click","a",function(e) {
e.preventDefault();
$('.mailShow').fadeIn(1500).html('This is the mailShow div');
});
});
<?php
while ($row = mysql_fetch_assoc($rsd))
{?>
<div class="each_rec"><?php echo $row['name_usr'];?> <?php echo $row['idcode_usr'];?></div>
<?php
}
if($total==0){ echo '<div class="no-rec">No Record Found !</div>';}
?>
I see two probable issues:
1. You didn't use $(document).ready(); and put your code outside of it. Then you need to put the above code to docyument ready.
2. You inseert links dinamycaly - from ajax query. Thus you need to use jquery delegates instead.