How to add a href using jquery

1

I am trying to add a href to a div I have the following code:

<div class="field field-name-field-palabras-clave field-type-text field-label-inline clearfix">
<div class="field-label">Tags:</div>
<div class="field-items">
    <div class="field-item even">libreta
        <div class="icon-more_link"></div>
    </div>
    <div class="field-item odd">pluma
        <div class="icon-more_link"></div>
    </div>
</div>

and I want to do this:

<div class="field field-name-field-palabras-clave field-type-text field-label-inline clearfix">
<div class="field-label">Tags:</div>
<div class="field-items">
    <div class="field-item even">
        <a hef="search/libreta">libreta</a>
        <div class="icon-more_link"></div>
    </div>
    <div class="field-item odd">
        <a hef="search/pluma">pluma</a>
        <div class="icon-more_link"></div>
    </div>
</div>

Could you please help me?

    
asked by skycomputer2 04.09.2016 в 03:13
source

4 answers

2

I think what you're looking for in a function similar to this

$('.field-item .even').click(function() {
    valor= $(this).html();
    $(this).prepend('<a hef="search/'+valor">'+valor+'</a>');
});

Although if you want to redirect the page directly by clicking on the tag, you could do something similar to this

$('.field-item .even').click(function() {
    valor= $(this).html();
   $(location).attr('href',"search/"+valor);
});
    
answered by 04.09.2016 в 10:46
0

You can use the function prepend

$('.field-item .even').prepend('<a hef="search/libreta">libreta</a>');
$('.field-item .odd').prepend('<a hef="search/pluma">pluma</a>');

more info in jquery prepend

    
answered by 04.09.2016 в 03:36
0
$(function(){
  $('#a').attr('href','TuLink');
});
    
answered by 04.09.2016 в 15:28
-1
var link="www.google.com"
 $('#tuEtiqueta').attr('href', link);

There you can change or add attribute href to any element you need it

    
answered by 12.05.2018 в 14:26