adding parameters to the url using jquery

Posted on 2016-10-19
Last Modified: 2016-10-19
Hi experts

I'm using jquery with a html page.

Right now I'm adding an image to my  page my using .prepend like this.

var myimage = $('<a href=""><img class="testclass" src="Images/testimage.jpg" /></a>');

This image is added correctly to my page and the hyperlink works.  It takes me to:

What is the syntax to add some parameter to the url that are stored in a variable?

So i would have these variables

var parameter1 = "apple"
var parameter2 = "peach"

But I need the syntax to add these variables  to this url in the snippet below:

var myimage = $('<a href=""><img class="testclass" src="Images/testimage.jpg" /></a>');

so I want the hyperlink url to go to:

Anyone know the syntax?
Question by:maqskywalker
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 22

Accepted Solution

Kim Walker earned 500 total points
ID: 41851399
so I want the hyperlink url to go to:
This is an invalid URL. If you wish to add a query string to the URL, you must begin with a question mark and follow it with your key=value pairs, separated by ampersands. For example:

Open in new window

The simplest way to add the variables to the URL is to hard code them.
var myimage = $('<a href=""><img class="testclass" src="Images/testimage.jpg" /></a>');

Open in new window

But I imagine the variables are variable, so you would need to concatenate the URL string.
var parameter1 = "apple";
var parameter2 = "peach";
var myimage = $('<a href=""+parameter1+"&parameter2="+parameter2"><img class="testclass" src="Images/testimage.jpg" /></a>');

Open in new window


Author Closing Comment

ID: 41851403

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question