Hi, my problem is as follows.

My aim is when a user clicks 'comment' for a post, a <div> appears with the form, which I can get working just fine.

However as moving on with my development, now when a user clicks 'comment', parameters are passed to change the url to add on the id of the selected post so I can work with each post as a separate element. E.g.


Now, because the page changes when 'comment' is clicked to to add the id accordingly, the hidden div never gets shown!

I can see it slightly appear but as soon as the website applies the parameter, it disappears again.

What I need help with is making this div show regardless, but I still need the paramters to be passed to the URL!

Using event.preventDefault(); shows the div, but does not pass the parameters.

Any help would be appriciated!

Well, the only way to alter the query string (unless you are applying an anchor) is to reload the page. If you are trying to show the form before the page is reloaded, then you are going to need to change around the order of things here. Or, you could have the form shown based on a paramter that you set in the query string after the page has reloaded maybe?

Yeah that sounds right. So after I click comment for a post with a post_id of 3 for example, the url changes to www.website.co.uk/?do=comment&id=3 (which is currently not a problem). And then I would like the hidden form to be shown? Is this possible?

Yeah, check number 6 on this list of js/jQuery snippets. He has one there that grabs the query parameters in a query string. Then all you need to do is check if param=value exists, and then call your function.


var vars = [], hash;
    var q = document.URL.split('?')[1];
    if(q != undefined){
        q = q.split('&');
        for(var i = 0; i < q.length; i++){
            hash = q[i].split('=');
            vars[hash[0]] = hash[1];

You could create a function out of that and just call the function.

Thanks, I got that example working to alert out parameters, however I am not sure how to implement this to work for my problem? As to show the hidden div, I would not be able to have any paramters as the div will only show on the same page?

Edit: how about if I change the ?do=comment&id=2 to #do=comment&id=2 as this with the hash, it still shows the hidden div, and then use example no.3 from the website you provided to get the #paramters?

Well, the whole point of all of this was because you needed to modify the query string for some reason. If you are able to use a hash instead for your operation, then you don't need to reload the page at all.

I think the problem is confused, I do not want to modify the query string, I just cannot use my jquery script to show a hidden div when comment is clicked because clicking comment leads to a reload in page.

So basically, when I click comment this happens: /newsfeed.php changes to /newsfeed.php?do=comment&id=1.

And I would like my jquery script to show the hidden div after the reload as at the moment it literally flashes and disappears due to the reload

The page must be reloading when someone submits a comment? Okay, then use the first script to grab the parameters in the query string.

You should have a function to show the div that has your comment form. You would check the query string first onload to see if it has your paramter=value, and if true call your function to show the div. You would also call the same function if someone was to click on your comment button.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.