Getting 'id' of the current visible DIV - php

I have around 6 div on a page and only one of them needs to be visible at a time. During page load, I hide all the other divs except the first one.
Here's my HTML structure.
<div id="A1">
</div>
<div id="A2">
</div>
<div id="B1">
</div>
<div id="B2">
</div>
<div id="C1">
</div>
<div id="C1">
</div>
Here's what I do to hide them :-
$(document).ready(function(){
$("#A2").hide();
$("#B1").hide();
$("#B2").hide();
$("#C1").hide();
$("#C2").hide();
});
How would I get the ID of the visible div? I have tried :-
var current_div = $("div:visible");
current_div_id = current_div.attr('id');
Output :- Undefined
var current_div = $("div:visible");
current_div_id = current_div[0].id;
Output :- Blank
What am I getting wrong ?

Fixed it. Posting a solution to help future peeps.
It seems that jquery is unable to find the current div using my above code. What I did is, added a common class to all the divs. Like this :-
<div id="A1" class="divs">
</div>
<div id="A2" class="divs">
</div>
<div id="B1" class="divs">
</div>
<div id="B2" class="divs">
</div>
<div id="C1" class="divs">
</div>
<div id="C1" class="divs">
</div>
And now I modified my JS in this way :-
var current_div = $(".divs:visible");
current_div_id = current_div[0].id;
And it worked. So if you're in the same problem, remember to have a common class for all the divs.

Try this:
var current_div = $("div:visible").attr("id");
$( 'body' ).text( current_div );
http://jsfiddle.net/neowar2x/4/

well, your code is working actually, and I can get id using both methods. You're missing something else here, check console what else do you get.
just one issue, you won't get consistent results as you've duplicate id on your page C1, twice.
<div id="A1">
A1
</div>
<div id="A2">
A2
</div>
<div id="B1">
B1
</div>
<div id="B2">
B2
</div>
<div id="C1">
C1
</div>
<div id="C2">
C2
</div>
<p id = "one"></p>
<p id = "two"></p>
$(document).ready(function(){
$("#A1").hide();
$("#B1").hide();
$("#B2").hide();
$("#C1").hide();
$("#C2").hide();
});
var current_div = $("div:visible");
current_div_id = current_div[0].id;
$("#one").text("ID: "+current_div_id);
var current_div = $("div:visible");
current_div_id = current_div.attr('id');
$("#two").text("ID using second method: "+current_div_id);
Fiddle

Related

First jquery/ajax test not working

I'm trying to utilize jQuery/Ajax more in my web projects, as a test I have done:
HTML Code
<div id="main_content"></div>
<div class="panel panel-primary">
<div class="panel-heading">API Credits Left.</div>
<div class="panel-body">
<form id="frmAjax" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" class="form-horizontal container-fluid" role="form">
<div class="row form-group">
<div class="col-sm-12 text-right">
<button type="submit" name="submit" class="btn btn-default">Check the API credits...</button>
</div>
</div>
</form>
</div>
<div class="panel-footer"><b>DomDetailer</b> & <b>SeoKicks</b> Api credits left.</div>
</div>
Script
$(function() {
$(document).ready(function(){
$('#frmAjax').submit(function() {
$("#main_content").load('ajax-credits.php');
});
});
});
The ajax-credits.php contents:
<?php
echo "Loading...";
// http://site1.com/api/checkBalance.php?apikey=QRUE8VTQMD5Fj6&app=
$json1 = file_get_contents('http://site1.net/api/checkBalance.php?apikey=QRUE8VTQMD5F6&app=');
$data1 = json_decode($json1, true);
$valu1 = $data1[1];
// seokicks
$json2 = file_get_contents('http://www.site2.net/V1/inlinkData?appid=p2hFacSWU0&output=json&details=api_credits');
$data2 = json_decode($json2);
$valu2 = $data2->Overview->credits->available;
// display
stdmsg("Site 1 has <b>".$valu1."</b> credits left and Site 2 has <b>".$valu2."</b> credits left.");
?>
Very basic code (or so I thought) can anyone see what I have done wrong? Nothing is being displayed in the div , is there any issue?
Try removing echo "Loading"; and replacing
stdmsg("Site 1 has <b>".$valu1."</b> credits left and Site 2 has <b>".$valu2."</b> credits left.");
by
echo "<p>Site 1 has <b>".$valu1."</b> credits left and Site 2 has <b>".$valu2."</b> credits left.</p>";
(Whatever you echo in the PHP script called by a jQuery AJAX function is what gets sent back to the AJAX function, so that is what will get loaded into your HTML div.)

Get content of div by css class name

Using dom I want to get content from some specific div.
My Current situation is this : I want to get content from site link whose html structure is as below. I want that css class data for my local database. I think this is possible only using dom.
Here is the code :
<div class="listing-info-right ">
<div class="list-card-field name-address">
<div class="list-card-user">
Mr Bimal Kumar Agarwal
</div>
</div>
<div class="list-card-field name-address">
<div class="list-card-phone">
+91-9386750700,+91-612-2530310,+91-612-2530311
</div>
</div>
<div class="list-card-field name-address">
<div class="list-card-email">enquiry#elementguestline.com</div></div>
<div class="list-card-field name-address list-card-border">
<div class="list-card-location">28,, Nalini Aparment 4th Floor, Kidwaipuri, Fraser Road, Patna, 800001, Bihar</div>
</div>
</div>
I want to fetch data of div class named : list-card-user, list-card-phone and list-card-location.
Before I had done coding using dom with css id, but could not get anything with class name..!
Please help me with some code.
Thanks in Advance.
You can select your div with document.getElementsByClassName in Javascript. This will return an array of all the elements with that class, if you only have one occurence then simply get the first node.
var div = document.getElementsByClassName("list-card-user")[0];
The div variable now points to your div, you can use a bunch of functions on it to get different things. For example, if you want its content, you can do this :
var content = div.innerHtml;
Edit I used Javascript because your original question used the Javascript tag, which has been removed now with an edit.
This is the simple one, perhaps this helps you :)
<div class="listing-info-right ">
<div class="list-card-field name-address">
<div class="list-card-user">Mr Bimal Kumar Agarwal</div>
</div>
<div class="list-card-field name-address">
<div class="list-card-phone">+91-9386750700,+91-612-2530310,+91-612-2530311</div>
</div>
<div class="list-card-field name-address">
<div class="list-card-email">enquiry#elementguestline.com</div>
</div>
<div class="list-card-field name-address list-card-border">
<div class="list-card-location">28,, Nalini Aparment 4th Floor, Kidwaipuri, Fraser Road, Patna, 800001, Bihar</div>
</div>
</div>
Output:
<div id="myNamez"></div>
<div id="myPhonez"></div>
<div id="myLoc"></div>
Using jQuery:
<script>
$(document).ready(function () {
var namez='list-card-user';
var phonez='list-card-phone';
var locationz='list-card-location';
document.getElementById('myNamez').innerHTML='namez';
document.getElementById('myPhonez').innerHTML='phonez';
document.getElementById('myLoc').innerHTML='locationz';
});
</script>
Here's the demo

how to i passing row from mysql database into div popup via using javascript or jquery

i need help to passing row from mysql database into div popup window?
how do i passing product id into popup div which i called through View?
For Example I Want Like This
i want to fetch product details into `product quick view popup window`
through product `ID`?
Here My Code
<div class="latest_products_sec">
<div class="latest_products">
<div class="title_box">
</div>
<!--Latest Products Slider -->
<div class="latest_pro_box">
<h4>Latest Shirt's Collection</h4>
<div class="latest_products_slider">
<?php
$queryshirt=mysql_query("select image,id from products
where cid='1' LIMIT 5") or die ('die shirt query');
while($rowshirt=mysql_fetch_array($queryshirt))
{
echo '<ul>';
echo '<li><img src="admin/'.$rowshirt['image'].'"
width="225" height="300" alt="" />
View</li>';
echo '</ul>';?>
}
?>
#shirt Div Popup Window
<div id="shirt" class="proquickview">
<span class="close_btn" onclick="parent.jQuery.fancybox.close();">Close</span>
<h2 class="title">quick view</h2>
<div class="quickviewinfo">
<?php
// I Need To Get ID Here
echo $id=$_REQUEST['id'];
?>
<div class="quickviewinforight">
<div class="titlerow">
<h2>Latest Shirt's Collection</h2>
<div class="start_ratings">
<div class="start_rating">
</div>
</div>
<div>
<div class="quick_review">
<h3>Quick Discription</h3>
<p>TEST.</p>
</div>
<div class="qty_row">
<div class="qty_field">
<label>Select Quantity</label>
<span>
<select name="S">
<option>5</option>
</select>
</span>
</div>
<br class="clear" />
<span class="total_price">Price <strong>$88.00</strong>
<del>$102.00</del></span>
ADD TO CART
</div>
<div class="total_price_row">
</div>
</div>
</div>
</div>
Javascript For Popup Window
$(document).ready(function() {
$(".view_btn").fancybox({
'titlePosition' : 'inside',
'transitionIn' : 'none',
'transitionOut' : 'none'
});
});
$(document).ready(function(){
// Cufon Functions //
Cufon.replace ('.latest_products_slider ul li a.view_btn',{hover:true});
});
It's hard to tell what is wrong as you have only given part of the story.
Fundamentally you need:
Action by the user (usually a click) triggers AJAX call sending a unique ID onclick="updatefunction(this.id) in a SPAN or DIV will do the trick:
<SPAN onclick="updatefunction(this.id)" id="1234">Your Stuff to update</SPAN>
The JS function updatefunction must send the detail/ID to a separate PHP script (you can use the original but this just makes it clunky) which queries your database on the ESCAPED data you send.
Once the data is ready, the JS will update the window/pop it up.
AFAICS you are missing the extra bit that actually does the querying or not applying a suitable onclick.
Why not have a look at the excellent W3Schools Ajax tutorial - it will help though does not use jQuery.

php, jquery only first id working normal

I have the following code in .php (code is edited for easier understanding).
while($row = mysql_fetch_array($biznis)){
<div id='listItem'>
<div id='listPic'>
<img src='../../social/images/avatar/empty_avatar_full.jpg'></img>
</div>
<div id='listCont'>
<span>abcde<i>(request pending)</i></span>
</div>
<div id="listInfo">
<span id='info'>More info</span>
</div>
<div style='clear:both;'></div>
<div id='moreInfo'>
<span>Invitation sent: xx-yy-zzzz</span>
</div>
</div>
<div style='clear:both;'></div>
}
So the code is nothing special, I just get all records from the table lined up.
So now, here is the problem, as you can see, I use the same ids for the same elements.
Jquery code
$(document).ready(function () {
$("#moreInfo").hide();
$("#info").click(function(){
$("#moreInfo").slideToggle();
});
});
OK so here are now the problems. Only first div (#moreinfo) is hidden, all the others are shown. And only first span (#info) is toggling the slider.
I tried with putting the counter for ids, and jquery each function, but nothing realy worked as i imagined.
Ty, Sebastian
Element IDs should be unique identifiers for that element. Use classnames for generic selectors:
<? while($row = mysql_fetch_array($biznis)): ?>
<div class="listItem">
<div class="listPic">
<img src="../../social/images/avatar/empty_avatar_full.jpg"></img>
</div>
<div class="listCont">
<span>abcde<i>(request pending)</i></span>
</div>
<div class="listInfo">
<span class="info">More info</span>
</div>
<div style="clear:both;"></div>
<div class="moreInfo">
<span>Invitation sent: xx-yy-zzzz</span>
</div>
</div>
<div style="clear:both;"></div>
<? endwhile; ?>
And change your jQuery to:
$(function() {
$('.moreInfo').hide();
$('.info').click(function() {
$('.moreInfo').slideToggle();
});
});
Edit
$('.info').click(function() {
$(this).parent().siblings('.moreInfo').slideToggle();
});
use class and not ids because you're in a while loop. An id exists only once.
And update your JS.

most acceptable way to put dynamic html on a page

I would like to append some html to a div when a button is clicked. The only thing that needs to be dynamic about it is that it needs to have a different id the the otherwise identical html above it. So if I have a structure like this:
<div id="container">
<div id="div1" class="inner">
<p id="P1">Some content</p>
</div>
<div id="div2" class="inner">
<p id="P2">Some content</p>
</div>
</div>
and to the end of container i would like to append
<div id="div3" class="inner">
<p id="P3">Some content</p>
</div>
What would be a good method to store this HTML? Keep it all in a php page and post the number the new div's id should be incremented to? Or is is smarter to put the html inside a string, and have a regular expression increment all of the numbers. Or is there some incredibly obvious way to do this that I've completely missed.
Jquery is an option, as that's already on the page.
Also, if this question seems too open-ended, please let me know in the comments how I can change it before closing it.
Thank you very much.
Based on your answers to comments so far (i.e. that the id's aren't really important, and that you will have input elements in the content), I would say that you would want to redo your html to look like this:
<div id="container">
<div class="inner">
<p><input type="text" name="order_item[]" value="" /></p>
</div>
<div class="inner">
<p><input type="text" name="order_item[]" value="" /></p>
</div>
</div>
Note there are no longer id's and I have used array notation for your input elements such that they will be posted as an array to the receiving script (eliminating the need to increment counters in javascript or parse different posted variable names into a usable array in PHP on the server).
And then use something like this in jQuery
$('#button_id').click(function() {
$('#container > div.inner:last').clone().val('').appendTo('#container');
});
Put the HTML you want to add each time into a hidden div, then use jQuery to copy and add that to the relevant location. For instance:
<div id="appendContents" style="display:none;">
<div class="inner">
<p id="P3">Some content</p>
</div>
</div>
then jquery:
var shownDivs = <?php echo $numberOnScreenFromBeginning;?>;
$('#theButtonYouWantClickable').click(function()
{
var newContent = $('#appendContents').clone();
newContent.$('div.inner').get(0).id = 'div'+shownDivs;
newContent.$('div.inner p').get(0).id = 'P'+shownDivs;
// Where does P content come from??
shownDivs++;
$('#container').append(newContent);
});
Try this Please: Working Demo http://jsfiddle.net/eNXmr/
In the code below it will calculate the lenght of the div hence dynamically set the id as 1, 2, 3, and beyond.
Hope it fits you need :)
API used: http://api.jquery.com/append/
Code
$('#hulk').click(function() {
alert("Total div inside ==> " + $('#container').find('.inner').length);
var next_num = parseInt($('#container').find('.inner').length) + 1;
$('#container').append(' <div id="div' + next_num + '" class="inner"><p id="P' + next_num + '">Some content</p></div>');
});​
​
HTML
<div id="container">
<div id="div1" class="inner">
<p id="P1">Some content</p>
</div>
<div id="div2" class="inner">
<p id="P2">Some content</p>
</div>
</div>
<input type="button" value="Click hulk" id="hulk" />​

Categories