I am attempting to write some jQuery code that will expand a paragraph when a link is clicked and once expanded present another link that will allow the paragraph to be collapsed. These paragraphs are all generated within a foreach loop and I am having trouble selecting the correct paragraph because I am not sure of the best way to create unique IDs to pass back to jQuery because there in a loop.
Here is my view code:
<? foreach ($e['comments'] as $comment) : ?>
<div class="comment">
<p class="collapsed">
<?=character_limiter($comment['comment'], 100) ?><br />
Show More
</p>
<p class="expanded">
<?=$comment['comment'] ?>
<a href="#" class="collapse" >Show Less</a>
</p>
</div>
<? endforeach; ?>
And here is what I have so far with jQuery:
$(document).ready(function()
{
$("p.expanded").hide();
$("a.expand").click(function()
{
$(this).parent().hide();
return false;
});
});
I am able to hide the correct when clicking "Show More", however I am lost as to choosing the correct "expanded" paragraph and then implementing the opposite for collapsing.
My thoughts so far are that I need to somehow make the elements in question have unique ideas. The $comment array does have an 'id' value that code be appended to an id name for each attribute making them unique, but I am still confused about how to properly select things with jQuery.
IDs aren't the only way to target individual elements - you can target elements around the current jQuery element using a variety of methods, just like you have with parent.
You should be able to target the corresponding "expanded" block using siblings():
$("a.expand").click(function()
{
$(this).parent().hide();
$(this).parent().siblings('.expanded').show();
return false;
});
Related
I have implemented the image Drag an Drop as seen in the following example: http://jqueryui.com/droppable/#photo-manager. I have added the ID's for each image element so that I can create a list of the Trashed elements and another for the ones that stay untouched.
I could really use some help on getting the selected and non-selected id's into PHP
variables after the dropping took place.
Starting UL gallery (Draggable) Image element with id e.g.:
<ul id="gallery" class="gallery ui-helper-reset ui-helper-clearfix">
<li class="ui-widget-content ui-corner-tr">
<img src="img/gler.png" alt="lake" width="96" height="32" id="2">
Delete image
</li>
</ul>
Trash DIV (Droppable)
<div id="trash" class="ui-widget-content ui-state-default">
<h4 class="ui-widget-header"><span class="ui-icon ui-icon-trash">Trash</span> Trash</h4>
</div>
Using javascript you'll need to build an array of ids in each container like this:
var selected = [],
unselected = [];
$('#gallery img').each(function(){
selected.push($(this.attr('id'));
});
$('#trash img').each(function(){
unselected.push($(this.attr('id'));
});
You may trigger this action using an event such as after each drop or when a user clicks a done button, whatever. Now pass those arrays to the server however you like(refresh the page or ajax call).
I'm using Drupal CMS. I'm a newbie in Drupal and PHP.
In one of my *.tpl.php file I've a PHP code snippet as follows:
<div class="form-section">
<h3>Job Alerts: jobs delivered to your inbox! <strong>(optional) </strong> <span class="info-ico"><em><?php echo bfstring('tooltip_register_job_alerts'); ?></em></span></h3>
<?php $alerts = bevforce_get_user_option($user->uid, 'alert', false); ?>
<ul class="jobs-alerts-list">
<?php foreach ($alerts as $a) : ?>
<li><?php echo $a['value']['alert_name']; ?> edit</li>
<?php endforeach; ?>
</ul>
<p><!--<a class="popup-loader" href="<?php echo url(); ?>?bf-ajax=create-job-alert&page=register"><strong>Create Job Alert</strong></a>-->
<a class="popup-loader" href="/?bf-ajax=create-job-alert&page=register">Create New Job Alert</a>
</p>
</div>
I'm not getting what's the purpose of data-oid="<?php echo $a['oid']; ?>" in anchor tag(<a>). I've never seen such attribute anywhere in <a> tag.
Following is another code snippet from my PHP code :
<li><a class="popup-loader" href="/?bf-ajax=delete-job-alert&eid=&oid=0">Remove</a></li>
If I hover the mouse cursor over the hypertext Remove, I'm getting the following URL:
xyz.com/bf-ajax=delete-job-alert&oid=3805462
How could this happen that I'm passing the value oid=0 in query string but it is showing some different value when I hover the hypertext? Is this happening due to the data-oid attribute we used in <a> tag above?
So in short my doubt is what's the purpose of attribute data-oid in <a> tag and how the value is getting changed from the value I set in the code?
Can anyone clear my above doubts?
Thanks in advance.
The data-* attribute is primarily for JavaScript in HTML 5. See: http://html5doctor.com/html5-custom-data-attributes/
Using the jQuery library, it's really easy to reference a data attribute: $("#someLink").data("name") for something like Click Me -- the .data("name") is simply for data-name.
So I have a website that navigates by scrolling through a pane of DIVs that's wrapped inside a main DIV via. JQuery/javascript: http://plugins.jquery.com/project/ScrollTo
E.g.
<div id="content" style:"overflow:hidden; width 800px;">
<div id="home" class="page"></div>
<div id="about" class="page"></div>
<div id="support" class="page"></div>
</div>
It navigates and scrolls fine, but attempting to provide dynamic URLs for the pages without breaking the scrolling feature (e.g. mywebsite.com?p=home) brings a bit of trouble.
So depending on what the GET request returns, I want the PHP script to automatically set the scroll position on page load; as the scroll bars are hidden, and can only be set via. javascript.
What is the best method for this?
Probably something like this
<script>
var goTo = '<?php echo (isset($_GET['p']) ? $_GET['p'] : "default_value"); ?>';
(function($){
$(document).ready(function(){
functionThatScrolls(goTo);
});
}(jQuery));
<script>
I would do it like this, just print the value of the $_GET['p'] into the script, just make sure to print a default value, and maybe sanitize the value of p someone could insert something into it.
hope it helped.
May I suggest simply using plain old anchor tags?
The way you've described your site doesn't seem to need all this js magic in order to achieve the effect you're looking for...
<div>
<a name="home">
<div>
</div>
</a>
<a name="pix">
<div>
</div>
</a>
<a name="about us">
<div>
</div>
</a>
<a name="contact">
<div>
</div>
</a>
</div>
Then, links to http://www.mywebsite.com/#home will go do what you're looking for, plus google will index it as a subsection of http://www.mywebsite.com/
I think if you put a ! before your anchor tag names, google will actually index each as a separate page.
EDIT: Go here, and scroll down to "Step-by-step guide".
Here is something I use to get the $_GET vars:
function getQueryParams(qs) {
qs = qs.split("+").join(" ");
var params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}
return params;
}
var $_GET = getQueryParams(document.location.search);
$(document).ready(function(){
$('html,body').animate({
scrollTop: $('#'+$_GET.p).offset().top},
'slow');
});
Ok,
Firstly, if you click on the questions link at the top of this page, each question has some buttons at the bottom that pertain to the question. when you mouseover them it shows more about the button. How is this done? I want to do this on my site.
So basically, i am using a php while loop to echo listitems's queried from a users id in mysql.
each listitem contains some more block and inline elements. some of those block elements have onmouseover/mouseout events attached to them. yet if i use the same class name on those elements, when i trigger a mouseover, it triggers every element with that class name. I am new to php / js / jquery, and not sure on the best way to go about it. any help would be grand. Example below.
<ul class="ulclass">
<?php
$link = mysql_query("SELECT * FROM table WHERE id='".$_SESSION['id']."' ORDER BY lid");
$i = 1;
while ($row=mysql_fetch_assoc($link)) {
$ico = $row['url'];
echo '
<li>
<a href="'.$row['url'].'" target="_blank" >
<div class="title">'.$row['title'].'</div>
</a>
<div onclick="/*here i want to change the next div.css display to block*/">
<img src="something.png" class="something_img"/>
<div class="drop_menu" id="drop_menu'.$i.'"
onmouseout="t=setTimeout(\'/*here i want to change this div.
css display back to none*/\',300);" >
<form method="post" action="" onmouseover="clearTimeout(t);">
<input type="hidden" name="deletetitle" value="'.$row['hash'].'"/>
<input type="submit" class="" name="delete" value="x"/>
</form>
</div>
</div>
</li>';
$i++;
}
?>
</ul>
let's fix some little things first. You don't really need to put all the HTML in a string, you can just do stuff like:
<?php
while ( $i < 10 ) {
?>
<li>Line number <?php echo $i; ?></li>
<?php
$i++;
}
?>
This way you will retain syntax highlighting and you won't have all kinds of problems that will arise from using string (like having to escape all single quotes etc.).
On the subject of JavaScript / jQuery – you shouldn't really use inline event handlers, such as onclick / onmouseover. It's really hard to maintain mixed up code, it's already enough there is HTML and PHP, don't add JavaScript to the same place. You should put in a separate file (or at least in a separate <script> tag before the closing </body> tag) and hook to the elements by their classes. I simplified your code a little, I am also not 100% sure what you wanted to achieve with the code you posted, but judging by the example of stackoverlow tag links, I will do something similiar:
<a href="'.$row['url'].'" target="_blank" class="tag">
<div class="title">'.$row['title'].'</div>
<div class="drop-out">Content of the drop-out.</div>
</a>
So, we have class tag for the link, and we want to hover it and see the internal element, and we take the mouse out it should disappear, let's see what jQuery we need for that (don't forget to add it to your page):
$('.tag').hover(
function () {
// `this` points to the DOM element we are hovering
$(this).children('.drop-out').css({
display : 'block'
, opacity : 1
});
}
, function () {
$(this).children('.drop-out').animate({
opacity : 0
}, 350, function () {
$(this).css('display', 'none');
});
}
);
Here's the example: http://jsfiddle.net/R6sYD/
jQuery methods used in this example:
http://api.jquery.com/hover/
http://api.jquery.com/children/
http://api.jquery.com/css/
http://api.jquery.com/animate/
Hope this helps.
I am using following a tutorial from here: http://www.shopdev.co.uk/blog/cookies-with-jquery-designing-collapsible-layouts/
This is the script I use:
<script type="text/javascript" language="javascript">
$(function() {
// SETUP:
// When the info banner is clicked:
$('#setup').click(function() {
$('#intro').css("display","none");
$.cookie('intro', 'collapsed');
});
// COOKIES
// Info banner state
var intro = $.cookie('intro');
// READ THE COOKIES
if (intro == 'collapsed') {
$('#intro').css("display","none");
};
});
</script>
The script hides the following div as the cookie is read:
<div class="feedback attention" id="intro">
Text goes here
<a id="setup" href="#">Ok I get it, please hide this</a>
</div>
Everything work great but when the page loads the div is shown for a split second. I guess the solution is to present two different pieces of markup serverside according to the cookie info. I have no idea how to go about this.
On page load, you could use php to check the cookie, and then add a hidden class. Something like <div class="<?= $_COOKIE['intro'] == 'collapsed' ? 'hidden':'' ?>">
Edit:
In CSS then, you can add something like .hidden { display: none; } and use jQuery to add or remove that class.
You could just do something like:
<?php
if($_COOKIE['intro'] != 'collapsed') {\
//echo div...
}
if you are using PHP:
<?php if($_COOKIE['intro'] != 'collapsed'){ ?>
<div class="feedback attention" id="intro">
Text goes here
<a id="setup" href="#">Ok I get it, please hide this</a>
</div>
<?php } ?>
To completely remove the div rather than just hide it.
check $_COOKIE array for 'intro'
if ($_COOKIE['intro'] == 'collapsed')
//...
simply, add some kind of "hidden" class to the div, or specify it like style="display: none;"
//edit
actually, by adding the "style" attribute you ensure that the div is not displayed as soon as get parsed, while using "class" property might cause interval while waiting for CSS file.
thus
<div<?= ($_COOKIE['intro'] == 'collapsed') ? ' style="display: none"' : '' ?>> ..</div>
is best here.