Modify href attribute via jQuery

0

I need to know how do I change the links and images of a page via jQuery.

I have to do dynamic updates on a page and I need to create a script for this, which changes images and links.

Example:

<a href="link1"> <img src="imagem1"></a>
<a href="link2"> <img src="imagem2"></a>

Type, in this example I know that href contains link1 , link2 and that tag img contains imagem1 , imagem2

I need to know if I can create a script that captures the information that the user types and crawls tags and automatically converts.

Imagine that the user places a link link, in which case he would change the href link1 , the same would be for the images.     

asked by anonymous 11.05.2018 / 14:55

2 answers

0

You can change with the attr () function of jquery:

$('#elemento').attr('href', 'link que será passado')
$('#elemento').attr('src', 'source da imagem')

I hope I have helped.

    
11.05.2018 / 15:52
0

To avoid typing errors it is good to use another way to choose which option to change. You could use select , for example.

Using select you can do this with

  • The val function to know
    • What is the value in a input where the user typed. Ex: var novovalor = $("input").val()
    • which option is chosen by the user (link1, link2 ...). Ex: $("select").val()
  • The attr function to modify the value of a property of an element. For example: $("a:first").attr("href", novovalor)
  • 11.05.2018 / 15:58