Ajax sending to php then echo in div - html

I have an ajax function that sends a variable - num - to a php script and then echos the value of num in a div with id "status".
First what happens is the user makes a post, i run all the necessary queries and display the users posts in a div called "posts" (use a while loop so each post is in a div with the same id but the div's are under one and other). I would like the ajax functionality to work in every one of these "posts" divs (each "posts" div contains the two js buttons witch - onclick - manage the ajax function as well as a "status" div)
However every time i click one of the buttons, lets say in the bottom most visible "posts" div displayed (the button clicked calls the ajax/js function), the function carries out in the top most "posts" div where the first "status" div is whereas it should be displaying in the bottom most div where i clicked the button.

EDIT: Sorry, I was assuming you were using jQuery. My bad.
You should be using classes instead of IDs. If you use an ID jquery will only have an effect on the first one in the DOM. IDs should only be used once per page.
Use classes, then reference the divs you want to edit not only by class name, by some sort of reference to the post div you are interacting with.
$('.posts .button').click(function() {
//do ajax stuff...
$(this).parent('div').find('.status').html(data);
}
Updated code:
// .posts should be the class of the posts div
// .button should be the class of the button on which you are activating your ajax
$('.posts .button').click(function() {
$.post('your ajax post URL', function(data) {
$(this).parent('div').find('.status').html(data);
})
});

Related

Robot Framework: wait for an element to (dis)appear based on a custom HTML property?

I have a div with a property panelname that changes depending on which view in a multi step process I am in (every panel has a Next-button).
It takes a few seconds after each click on Next for the next panel to load and render. This div has the same ID on every step instead panelname changes for each view.
I can read the value for panelname using Get Element Attribute but how do I use this in one of the Wait until page/for element, or similar, keywords?
The following should work, assuming you know each panel's name:
Wait Until Page Contains Element //div[#id='sameId' and #panelname='name2']
This the example HTML for the keyword above:
<div id="sameId" panelname="name2"></div>

Using POST without a form in jade and node.js?

I am trying to make a delete option for some items I have in a list. The way I am doing this is to give every item in the list a button or a link of some sort that will use POST to send the _id (from mongodb). As you can see in the code below (req.body.id), I'm trying to get the id form the request. However, the request does not have the id, because I don't know how to do this without using a form and a input field.
I've seen some ways to do it with javascript in PHP. But is there a more jade and node.js specific way?
router.post("/deleteitem", function(req,res) {
var db = req.db;
var collection = db.get("itemcollection");
id = req.body.id;
collection.remove({
"_id": id
}, function (err, removed) {
if (err) {
res.send("Error");
}
else {
res.redirect("editlist");
}
});
});
Thanks for the help.
Edit:
Client-side:
extends layout
block content
h1.
Items
ul
each item, i in editlist
li
p #{item.name} costs #{item.price}
button#btnSubmit(type="button",method="post",action="/deleteitem") Delete
a(href="/") Back to start
The button part is not very relevant though. Firstly I am not sure if it's possible to do it this way. Secondly I have not included the id in any way. I was just experimenting.
One way I could probably do it is to include the id in the url as a parameter. But that seems a bit more convoluted than just using POST and including the id in the request.
You need to expose the POST data to the req.body object using an html form, even if that takes the shape of just a button with some hidden data. You can do this using an input element alongside your button element inside of a form.
extends layout
block content
h1.
Items
ul
each item, i in editlist
li
p #{item.name} costs #{item.price}
form(action="/deleteitem",method="post")
input(type="hidden",id="id",value="#{item.id}")
button#btnSubmit(type="button") Delete
a(href="/") Back to start
This code assumes that each item has an id attribute.
With this you can get the id value via req.body.id in the next route that you submit to using the button.

Highlight text based upon incoming link

Is there a way to highlight text based upon an incoming link.
Here is what I am trying to achieve. I have a page that has several links to anchors on another page. In addition to going to the named anchor on the child page, can I also make the text of the anchor link on the child page bold.
i.e.
Parent Page
Dog
Car
Tree
Child Page
Dog-Lab
Car-Audi
Tree-Pine
So if you were to click on Car on the parent page, it would take you to the Car info on the child page, and Car would be bold (but Dog and Tree would not).
basically what you want to do is use a jquery or javascript function to get the anchorlink.
var url = window.location.href;
var anchorlink = url.split('#');
this will leave you with an array ["page url", "anchor"]
so you can use anchor[1] to get the string of the anchorlink.
then you can use jquery to find the element with an id that matches anchor[1] and set the font-weight:700
$(document).ready(function(){
$('#'+anchor[1]).css('font-weight','700');
})
or if you have a class for bold styling
$(document).ready(function(){
$('#'+anchor[1]).addClass('bold');
})

How to correctly call jQuery function that uses HTML elements?

I'm new to the use of jQuery so the problem I'm facing should be fairly straight forward. Basically what I'm trying to accomplish is load a variety of simple text-only pages within DIV elements of my site, and with a navigation bar hide/unhide these individual DIVs.
DIVs are correctly loaded the requested pages using an script block. However, what is not working correctly is toggling the visibility of these DIV blocks. I've narrowed it down to a jQuery function I've created which blocks the entire script call whenever I refer to any of the DIV blocks. Let me explain better with a code snippet.
This is is some very simple code that, on the click of a menu link, runs a hide function then shows the corresponding DIV element.
$( document ).ready(function()
{
console.log("document ready."); <-- does NOT get called with hideDivs()
$('#button1').click(function(){
hideDivs();
$("#page1").show();
});
$('#button2').click(function(){
hideDivs();
$("#page2").show();
});
});
This is the hideDivs() function, JUST above the ready function:
function hideDivs()
{
$("#page1").hide(); <-- These lines cause the entire
$("#page2").hide(); <-- <script> block to note get called.
}
Finally, page1 and page2 are created with a script block halfway inside the page:
<div id="page1"></div>
<div id="page2"></div>
<script>
$("#page1").html('<object style="overflow:hidden; width: 100%; height: 500px;" data="page1.php">').show();
$("#page2").html('<object style="overflow:hidden; width: 100%; height: 500px;" data="page2.php">').hide();
</script>
Why then is it that the top SCRIPT block fails with the hideDivs() function? I've tried placing it inside the $( document ).ready function with no change. Again, if the function is blank, or contains something simple like 'console.log' it works, but when referring to DIV tags it breaks.
Even stranger, the code that makes the function FAIL, WORKS if I simply rewrite the code as such:
$('#button1').click(function(){
$("#page1").hide(); <-- This works fine
$("#page2").hide(); <-- (page1 repeated to match function code)
$("#page1").show();
});
I have quite a few pages so I would much rather be able to use a function as not to have lots of repetitive code.
I have no errors displayed in my javascript console. I've looked closely at functions calls with StackOverflow and Google searches but couldn't spot a solution. I'm sure I've made a really silly mistake I'm overlooking, so any help would be much appreciated.
So instead of the whole function to hide your divs, you can simply put a class on each one and hide them by selecting that class. For example, each page Div give a class="clickablePages", and then do:
$(".clickablePages").hide();
that will simply hide all the divs that you have added the class to.
As for repeating all the button clicks for each button, you can simply do it in one function based on the id of the button. You can again put a class on all of the buttons as well, trigger the function by selecting the class and then grab the id you need within that function. something like this:
$('.buttonclick').click(function(){
var pageID = $(this).attr('id');
$("#page" + pageID).show();
});
In this case, if your buttons just had an id of '1' or '2' that matched the page number, it would only show the div for that page number. Hope that makes sense.

filling div using ajax?

I've recently created a website with a menu-bar to the left. My next step is to update the right side of the page with content based on what option you choose on the in the menu. I know you can use iframe but I was wondering if there is an alternative to it, like a more dynamic one!
Most of the menu options are input-forms, I've read about ajax-calls to fill a div but couldn't find a good tutorial on how to achieve it.
edit:
Here's a sketch http://imageshack.us/photo/my-images/16/smlithis.png/
Consider using JQuery. Handling Ajax requests is so much easier than using ordinary JS.
Documentation for the ajax function: http://api.jquery.com/jQuery.ajax/
Using the success callback (the function that is executed upon success) you can fill in your div:
$.ajax({
url: 'ajax/test.html',
success: function(data) {
$('.result').html(data);
alert('Load was performed.');
}
});
Instead of .result, point the selector to your main div. The .html() function fills your div with data, which is the data returned from the ajax request.
Edit: It's 2018. Use the Fetch API.
You can use jQuery
This is how your menu button will look like:
<a href='#' onclick='return fillDiv(1)'>GoTo1</a>
<script>
function fillDiv(pageNum){
$("#id_of_div_to_load_to").load("some_page.php",{ 'pahe_num': pageNum } );
return false;
}
</script>
It is just one of many ways to do it.
Ajax can get data from a server but it cannot fill anything. Ajax is just javascript used to communicate with the server. Javascript can take that data and insert data and create elements to fill that div.
You mean something like this:
How to update div when on select change in jquery
If you actually want to get the data dynamically from another source that would be an entire different matter.