9951 explained code solutions for 126 technologies


jqueryHow do I create a link using jQuery and JavaScript?


Creating a link using jQuery and JavaScript is a fairly simple process. To do so, you will need to use the attr() method. This method allows you to set attributes, such as the href attribute to create a link.

For example, the following code will create a link with the text "Click Here" that points to the URL "http://www.example.com":

$("#myLink").attr("href", "http://www.example.com").text("Click Here");

The code works by selecting the element with the ID myLink and setting its href attribute to the URL http://www.example.com. The text() method then sets the text of the link to "Click Here".

Code explanation

  • $("#myLink") - Selects the element with the ID myLink.
  • attr("href", "http://www.example.com") - Sets the href attribute of the selected element to http://www.example.com.
  • text("Click Here") - Sets the text of the link to "Click Here".

Helpful links

Edit this code on GitHub