I have 5 HTML buttons on a page. I want to pass the value of the particular button that has been clicked through a query string on second page through JavaScript. Please advise me.
3 Answers
I'm cheating and using jQuery, just to illustrate the concept.
You have your 5 buttons with their own unique IDs and all of them have a class of "button"
On clicking any of them, the browser will navigate to "http://mydomain.com/nextpage" with the "buttonclicked" parameter in the query string set to the ID of the button that was clicked.
$('.button').click(function(){
window.location.href = "http://mydomain.com/nextpage?buttonclicked="+this.id;
});
Interactive example: http://jsfiddle.net/6EZvG/
2 Comments
RussellUresti
If it has to be done with JavaScript, this seems like it would be the best idea -- though I think I would use the data-* attribute and put the query string inside that, something like data-qsv="query_string_value".
Chris Cherry
@RussellUresti that's a great idea, looser coupling
This only makes sense to me as non-javascript:
<form action="" method="get">
<input type="submit" name="button" value="button 1" />
<input type="submit" name="button" value="button 2" />
<input type="submit" name="button" value="button 3" />
<input type="submit" name="button" value="button 4" />
<input type="submit" name="button" value="button 5" />
</form>
That's plain old HTML's way of passing buttons on the QS, could you clarify / give example of your situation?