Solved

adding parameters to the url using jquery

Posted on 2016-10-19
2
28 Views
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="http://www.google.com"><img class="testclass" src="Images/testimage.jpg" /></a>');
$('.TestDiv1').prepend(myimage);

This image is added correctly to my page and the hyperlink works.  It takes me to: http://www.google.com


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="http://www.google.com"><img class="testclass" src="Images/testimage.jpg" /></a>');
$('.TestDiv1').prepend(myimage);


so I want the hyperlink url to go to: http://www.google.com&apple&peach


Anyone know the syntax?
0
Comment
Question by:maqskywalker
2 Comments
 
LVL 21

Accepted Solution

by:
Kim Walker earned 500 total points
Comment Utility
so I want the hyperlink url to go to: http://www.google.com&apple&peach
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:
http://www.google.com?parameter1=apple&parameter2=peach

Open in new window

The simplest way to add the variables to the URL is to hard code them.
var myimage = $('<a href="http://www.google.com?parameter1=apple&parameter2=peach"><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="http://www.google.com?parameter1="+parameter1+"&parameter2="+parameter2"><img class="testclass" src="Images/testimage.jpg" /></a>');
$('.TestDiv1').prepend(myimage);

Open in new window

0
 
LVL 1

Author Closing Comment

by:maqskywalker
Comment Utility
thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now