Wordpress post slideToggle with each() issue - javascript

I feel like I've been trying everything to get this to work, the problem is that I'm not that familiar with javascript nor jQuery.
My guess is that it's as simple as just adding one line, because I've almost got it to work.
I have a Wordpress blog which loops out numerous of posts, I've added unique ID's to everyone of them, which I'm able to grab the problem is that it doesn't work with slideToggle to show the content.
<script type="text/javascript">//<![CDATA[
$(window).load(function(){
$(document).ready(function() {
$(".toggleContainer").hide();
$('.show_hide').click(function() {
event.preventDefault();
var pid = $(this).data('id');
var cid = $(this).data('container');
$(pid).add(cid).each(function() {
$(cid).slideToggle(1000);
});
});
});
});//]]>
</script>
Then there's the container:
<div class="toggleContainer" data-container="<?php the_id(); ?>">
<?php do_action( 'woocommerce_before_single_product_summary' ); ?>
</div>
And the toggle:
<a href="<?php the_permalink(); ?>" class="show_hide" data-id="<?php the_id(); ?>">
If I use an alert on each of them it shows the correct ID, but it doesn't work when I want to toggle the content.

try: $('[data-container*='+cid+']').slideToggle(1000);

Related

Second button doesn't produce form in same div as first button

I have a php page with two divs. In the first div, users click a button and it brings up a form in the second div. When they submit the form, both divs refresh. All is great. But I want to add a second button to the first div to bring up a different form in the second div. I haven't been able to get this to work, either the first button stops working or the second one doesn't work. I tried making the second button a div and calling it by its ID and that didn't work. I'm an amateur and don't understand jquery very well (I think this is jquery anyway) and this is just for a project for some friends and I, so any help would be appreciated. I may just not be understanding how this works.
<script>
$(document).ready(function () {
$("button").click(function () {
var handle = "<?php echo $_SESSION['name'] ?>";
$("#infoblock").load("createhandle.html");
});
});
</script>
<div id="div1">
<button id="reservename">Reserve A Name</button>
</div>
<div id="div2"></div>
I found a solution here: https://stackoverflow.com/a/20074373/9157720
It ended up being an issue with syntax and using a pound symbol. This is the code that worked in case anyone else has this problem.
<script>
$(document).ready(function(){
var handle = "<?php echo $_SESSION['name'] ?>";
$("#reservename").click(function(){
$("#infoblock").load( "createhandle.html");
});
$("#uploadpic").click(function(){
$("#infoblock").load("uploadpic.html");
});
});
</script>
<button id="reservename">Reserve A Name</button>
<button id="uploadpic">Upload A Picture</button>
Consider the following.
<script>
$(document).ready(function () {
$("button").click(function () {
var handle = $(this).data("ref");
var target = $(this).data("target");
$(target).load("createhandle.html?handle=" + handle);
});
});
</script>
<div id="div1">
<button data-ref="<?php echo $_SESSION['nameA']; ?>" data-target="#infoblock">Reserve A Name</button>
<button data-ref="<?php echo $_SESSION['nameB']; ?>" data-target="#div2">Reserve B Name</button>
</div>
<div id="div2">
</div>
Your example is sort of ambiguous, so I wasn't sure what you were trying to accomplish. It's best to not mix PHP and HTML if you can. You can have a reference point in the HTML so that when you need to load something, in relationship to that button, you can pass that into a stand alone PHP Script and get the HTML you need back with .load().
Remember that PHP is executed before the HTML is presented to the Client. So the Session data must be present in PHP Memory when the page is initially loaded.
Reference:
https://api.jquery.com/data/
https://api.jquery.com/load/
jQuery $(this) keyword

PHP script JQuery grabs wrong number of elements

I've been using JQuery to show/hide elements based on login-status, however I've run into some unexpected results, here's the code:
<script>
$(document).ready(function(){
$("#uploadform").hide();
});
</script>
This hides the form if the user is not logged in.
<h2 id="needlogon"> You need to be logged in </h2>
<a id="needlogon" href="index.php"> Back to start </a>
These are to be shown only if the user is not logged in
<?php
if (isset($_SESSION['cid'])){
echo '<script>
$(document).ready(function(){
$("#uploadform").show();
$("needlogon").hide();
});
</script>';
}
?>
The uploadform is shown/hid as intented, but the needlogon elements are not hidden when the condition ($_SESSION['cid']) is set. The only way it works is if i change the ID's of the elements to needlogon1 and needlogon2, and changing the JQuery to:
<?php
if (isset($_SESSION['cid'])){
echo '<script>
$(document).ready(function(){
$("#uploadform").show();
$("#needlogon1").hide();
$("#needlogon2").hide();
});
</script>';
}
?>
I'm sure I'm just missing something silly, anyone know what it is?
For starters, your jQuery selector is off:
// $("needlogon").hide();
$("#needlogon").hide(); // you forgot the #
Having said that, you're using an ID -- which is supposed to be unique. You might want to change the two usages of needlogin to a class instead of id, and then your selector would be .needlogon like so:
$(".needlogon").hide();

How to change a href when using multiple variables in PHP?

So I didn't found what I was looking for in last two days.
To be clear I have a table "tracks" in my database.
So I can show from "tracks" the "demo" field wich is an audio file "url"..
<?= $tracks['demo']; ?>
I have multiple url's but then I need to replace the a href
<a href="<?php echo $tracks['demo'];?>"
in a logical way.
In simple terms it's like I want to click on button 1 that loads
url 1 into this a href with the ID, title field from that track.
When you press button 2 you need to load another url and replace url 1 with url2.
I tried many ways including javascript but it won't work.
Problem now is that when I list 4 items it always loads the latest "URL" i have posted in my source code. :)
When I echo out <?php echo $tracks['demo]; ?> on all items I can see the correct url's so the functionality to replace the url is my issue.
* I basically want to load many mp3 url's in a player I made in the footer of my website. It works by hardcoding the urls in the url field but this is not what it should be... *
Code:
<?php while($tracks = mysqli_fetch_assoc($muziek)) : ?>
<div class="col-md-2 viny" id="muziek">
<h4><?= $tracks['title']; ?></h4>
<img src="<?= $tracks['img']; ?>"
alt=" <?= $tracks['title']; ?> />
<p class="artist">Artist: <?= $tracks['artist']; ?></p>
</div>
<?= $tracks['demo'] = $audiourl ; ?>
<button type="button" onclick="play">Play</button>
</div>
<?php endwhile; ?>
*THIS LOOPS PERFECT FOR ALL ITEMS TRACKS ARE LISTED PERFECT *
In my player I have
<a href="<?php echo $audiourl;?>" ><?= $tracks['artist']; ?></b> - <?= $tracks['title']; ?></a>
function play(){
I'm not good at JS... And this is my issue because it now loads the latest output from the php loop...
}
I don't know how you are replacing it but
Why not pass the audio in the function. Here like this
<button type="button" onclick="play(<?= $tracks['demo'] = $audiourl ; ?>)">Play</button>
assign a id to anchor tag
<a id="someId" href="<?php echo $audiourl;?>" ><?= $tracks['artist']; ?></b> - <?= $tracks['title']; ?></a>
and in the play function, you receive it like this
function play(audioUrl){
}
and change the href like this in the play function
If you are using jquery
$('#someId').attr("href", audioUrl);
sometimes the above does not works. Can't remember why but if that does not works try this
$('#someId').href = audioUrl
If you are using javascript
document.getElementById('abcd').href = audioUrl
So I tried many things but I had many issues with the player it's js file using the same id's and so on.
The new logic I tried out seems to work:
<form action="" method="POST">
<input type="submit" value="Play" name="Play">
</form>
<?php
if (isset($_POST["Play"]))
{
echo $tracks['demo'];
}else{
echo ' ';
}
?>
There is more going on but I tried to write it down as simple as this to show the logic. All Track url's are hidden and when the page loads there is an empty $audiourl by default loaded in the a href from my audioplayer. By clicking the button play I show the url in the source but not on the site.
Then the latest $audiourl variable changes to this value.

Using Javascript function to hide button inside PHP

I am currently writing a website where I want to hide the the supervisor editing button if the supervisor field = N. I am able to get the query to display the correct output but I can't get the button to hide. Can anyone help me get it to hide whenever the page loads. I also under stand I should be using PDO's but we have not been taught that sadly.
HTML & PHP
<?php
$supervisorstatus = mysql_fetch_assoc($supervisorresult);
echo $supervisorstatus['supervisor'];
if($supervisorstatus !='Y') {
echo '<script type="text/javascript">',
'closesup();',
'</script>'
;
}
?>
JavaScript
function closesup(){
document.getElementById('supervisor').style.visibility="hidden";
}
Supervisor is the element id for the button.
This is how your code would be
<?php
$supervisorstatus = mysql_fetch_assoc($supervisorresult);
echo $supervisorstatus['supervisor'];
if($supervisorstatus['supervisor'] !='Y') {
echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#supervisor").hide();
});
</script>';
}
?>
I gave a example of implementation above but if you need to see it separate Here is the code to hide your supervisor id
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#supervisor").hide();
});
</script>
$supervisorstatus contains an array. You probably want: if($supervisorstatus['supervisor'] !='Y') {

Javascript show div when button is clicked

I realize that this is a common question and I have searched for solutions and the one that I'm trying to use (it works in jsfiddle) but when I tried to use it in my website it just won't work. I'm trying to show a div when a button is clicked. Am I doing something wrong? Maybe there's something wrong with the javascript? Or could it be I need to include a jquery js file? Thanks for your help in advance.
Here is the code I'm trying to use:
<script type="text/javascript">
$("#seeAnswer").click(function() {
$('#subtext').html($(this).next().html());
});
</script>
And I'm trying to use it with this:
<div class="back_button">
<button id="seeAnswer">See Answer</button>
</div>
<span>
<?php
/* foreach($row2 as $ans) {
$answer = $ans['answer'];
}
echo $answer;
echo "hi";
*/
?>
<?php foreach ($row2 as $ans) : ?>
<p><?php htmlspecialchars($ans['answer']) ?></p>
<?php endforeach ?>
<p>hi there</p>
</span>
<div id="subtext">
</div>
Use .parent() after .next() because you are referring to the next element after the button... but there is no element... so you want the next() of the parent element.
EDIT: as per new information about the <HEAD>, you are executing the code before the whole dom is ready... so you're actually applying the code to nothing. You need to wait for the DOM to be ready. Like this
$('docment').ready(function() {
$("#seeAnswer").click(function () {
$('#subtext').html($(this).parent().next().html());
});
});
Fiddle

Categories