jquery class, opens both of same - php

Okay so we have a mySQL while loop echoing out divs for a certain result.
Here is our code:
<div class="post">
<p>hello World</p>
<div class="contentSlide">
<p>Members image on left and comments on the right. Date floated on right, and link to user.</p>
<br />
<div class="contact">
<h3>Post a comment below</h3>
<p>My <label class="agentsHcard">Listings</label> <small>Only displays for logged in</small></p>
<form>
<input type="text" READONLY></input><br />
<textarea id="testTextarea" class="" type="text" name="comment" rows="3" style="background: none repeat scroll 0% 0% rgb(255, 255, 255); border: 1px solid rgb(51, 153, 255);"></textarea><br />
<div style="float:right;margin-right:40px;">
<button id="buttonsend" class="medium pink button" value="submit" name="submit" type="submit">Add Comment</button>
</div>
</form>
<br />
</div>
</div>
<div id="button" class="open">Comments(6) ▼</div>
</div>
Basically each result has a unique ID in our database, eg: 1, 2, 3, 4, 5
$(document).ready(function(){
reply=false;
$(".close").live('click',function(){
if(reply!==true){
$(".contentSlide, #contact").slideUp();
$(this).html("Comments(6) \u25bc").removeClass("close").addClass("open");
}
});
$(".open").live('click',function(){
$(".contentSlide").slideDown();
$(this).html("Close \u25b2 <span style='float:right;' onclick='reply=true;' class='reply' >Post a <a href='javascript:void(0);'>comment</a></span>").removeClass("open").addClass("close");
});
$(".reply").live('click',function(e){
$("#contact").slideDown(function(){reply=false;});
$("#button").html("Post and Close \u25b2").removeClass("open").addClass("close");
});
})
We are looking for a way to do this so that each div is given a unique ID, which is passed to the javascript and opens that relevant div.
As it stands, if we have 2 of these results, both of them open when the open class is clicked (obviously).
We know how to do it by duplicating the JS and giving a special ID to each one, but we're looking for a cleaner way by only using the JS once and making it global.
How can we make it so we only have one piece of javascript, and it runs off an ID? eg:
<div class="contentSlide" id="4">
Thank you.

If you want a single piece of Javascript, then id may not be the way to go. In order to operate on DOM elements related to the clicked element, you might want to use relative traversal functions such as closest(selector) to navigate up to the current target's ".post" div, then traversing back down to the elements to be manipulated:
$(".close").live('click',function(){
if(reply!==true){
$(this).closest(".post").children(".contentSlide, #contact").slideUp();
$(this).html("Comments(6) \u25bc").removeClass("close").addClass("open");
}
});
If you did go with an Id based mechanism, you would probably have to do some string manipulation and have the element id's be the original id with a prefix/postfix (i.e. button_4, contact_4, etc.)

You could try something like
$('.openthis').click(function(){
$(this).slideToggle();
});
On a semi-unrelated note, IDs must begin with a letter.

Create a function that accepts a JQuery item as its argument and binds the relevant actions to it. Then use a selector to find all items matching that class, and use each with that function.

Related

Setting the target of an onclick event to a specific location

I split my body content in two parts. The left one has a map and buttons. When I click on the button, I get the result from Arad.php in another window.
How can I set the target to the second half (split right) of my body?
<body>
<div class="split left">
<div class="centered">
<h2>Button on Image</h2>
<p>Add a button to an image:</p>
<div class="container">
<img src="Harta_Romaniei.jpg" alt="Harta_Romaniei" style="width:100%">
<button class="btnarad"; onclick= "window.location.href='Arad.php'"; Target="split right">Arad</button>
<button class="btntimisoara">Timisoara</button>
</div>
</div>
</div>
<div class="split right">
<div class="centered">
<h2>Information</h2>
// I want the information here!
</div>
</div>
</body>
It doesn't have much to do with PHP, you should really do it with HTML + JavaScript.
The window.href.location will always redirect you to another route, and will never do what you want.
You'll need something like this: Simple Load an HTML page from javascript based on window width

Get contents of a div infront of another div it contain

When see more button is clicked, second passage shows but there is a gap in between them as per shown in the screenshot. I want the second passages to continue with first one
function ClickParent(ClickElem,ElemFind)
{
$("."+ClickElem).click(function() {
var GetGrp = $(this).parents(".group");
GetGrp.find("."+ElemFind).fadeToggle();
});
}
// Find one level
ClickParent('name','moreinfo');
// Find another level
ClickParent('moreinfo','evenmore');
<div class="group">
How are you?
<div class="moreinfo" style="display: none;">
Fine
</div>
<div class="evenmore" style="display: none;">
Even more stuff.
</div>
</div>
How to get result from How are you?? Fine answer should be next to the question with no space.
I get now what you want for your project. You wanted a "Read more" function for your articles.
Using <div></div> to hide your continued text/string causes a new line. You can just replace it with <font></font>.
<!-- FIRST ARTICLE -->
<div class="group">
This is the first article that would
<font class="evenmore" style="display: none;">
show more information about this first article.
</font>
<div class="moreinfo">
See more
</div>
</div>
<!-- SECOND ARTICLE -->
<div class="group">
This is the second article that would
<font class="evenmore" style="display: none;">
show more information about this second article.
</font>
<div class="moreinfo">
See more
</div>
</div>
Then let us create a script that when "See more" is clicked, it will hide this "See more" and would show the content inside <font></font>.
$(document).ready(function(){
$(".moreinfo").click(function(){ /* WHEN SEE MORE IS CLICKED */
var elem = $(this);
$(this).fadeOut(200).hide(); /* HIDE THE CLICKED SEE MORE */
elem.closest("div.group").find('font.evenmore').fadeIn(200).show(); /* SHOW THE CONTENT OF THE CLOSEST FONT */
});
});
Here is a fiddle. But if you want to hide the shown details, we can add a "Show Less". Check this second fiddle.

How to draw text on top of image HTML?

I need to draw text in top of the image this the look like
when the page load i need to display price in side the photo.I try it like this
<button class="btn btn-danger" id="buy-btn" data-toggle="modal" data-target=".package-buy-modal">BOOK NOW</button>
<img id="price-tag" style="position: relative;width: 100%; /* for IE 6 */" src="<?php echo base_url()?>assets_profile/img/price.png">
<h2 style="-o-transform: rotate(32deg);-moz-transform: rotate(32deg);-webkit-transform: rotate(32deg);">$<?php echo $car_data['Charges']; ?></h2>
But it didn't work as i expected.how can i do this.if there is another easy way to this ?
Here is an example of using background-image within a div so that you can put more inside the div, ie)text
HTML:
<div id='image' src='http://i.stack.imgur.com/1DZ6X.jpg'>
<h2 id='price'>Price: $10</h2>
</div>
CSS:
#image {
width:243px;
height:163px;
background-image:url('http://i.stack.imgur.com/1DZ6X.jpg');
}
check it out here
Add a z-index value to the <img> and the <h2>. For example, z-index:98 on the <img>, and z-index:99 for the <h2>. (A higher value because you want it on top).
I would also recommend moving all styles to a css file, rather than use the inline style attribute.

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" />​

Why hidden input is affecting my layout? LF Explanation

I needed to use an hidden input to transfer some IDs to the page for each block.. whatever.
I have the following code :
<div id="shipping_box" class="formSep well">
<div id="default_shipping_box" class="shipping_box row-fluid">
<div class="span1">
<input type="hidden" name="tracking_id" value="" />
</div>
</div>
</div>
This code work well and the result is what I expected.
If I do this :
<div id="shipping_box" class="formSep well">
<div id="default_shipping_box" class="shipping_box row-fluid">
<input type="hidden" name="tracking_id" value="" />
<div class="span1">
</div>
</div>
</div>
The layout is not respected. See this picture for the demostration :
Can someone explain why to me ? Hidden input aren't suppose to be "hidden" so they shouldn't affect the layout ?
jsfiddle : http://jsfiddle.net/t9M3C/
Near line 285
Is because you have a css rule (in bootstrap.min.css file) that match the firs-child element (but only if has a class*="span") inside the default_shipping_box div.
.row-fluid [class*="span"]:first-child {
margin-left: 0;
}
So, if you put your hidden input inside the div#default_shipping_box and before the first span, then that rule is not styling the div.span1 and thats why your template is been afected.
You can fixed adding a simple css rule to the same file...
.row-fluid .span1{margin-left:0 !important;}
The important, is because you have more files who overite this rule (ex. in bootstrap-responsive.min.css)
Good luck, and i hope it helps
cheers,
Gmo.-
EDIT:
Too slow XD.
Answered while writing ... I agree with the reason explained above.
Using Google Chrome's Inspest Plugin, when you move the input this class:
.row-fluid [class*="span"]:first-child {
margin-left: 0;
}
Gets removed.
This is because in this:
<div id="shipping_box" class="formSep well">
<div id="default_shipping_box" class="shipping_box row-fluid">
<input type="hidden" name="tracking_id" value="" />
<div class="span1">
</div>
</div>
</div>
This : <div class="span1"> is not the first child, this: <input type="hidden" name="tracking_id" value="" /> is.
and in your CSS this is that default class for [class*="span"] is:
[class*="span"] {
float: left;
margin-left: 30px;
}
So use this for example :
.row-fluid .span1 {
margin-left:0 !important;
}
Hope this helps.
Bootstrap has some CSS that will set the left-margin of the first of the child to 0, if the class contains span:
.row-fluid [class*="span"]:first-child {
margin-left: 0;
}
When the hidden input is put above the first span div, the above margin-left: 0; property will not be applied.
The following image shows that when the hidden input is before, then the first span class has a left-margin.
This shows that when the hidden input is after the div, that there is no left-margin.
EDIT: I seem to have a been beaten twice, while I was getting the screenshots to illustrate the difference!

Categories