How to replace an attribute of an HTML snippet that is as a string?

2

I have a variable that contains several HTML codes. This variable is already mounted, what I have to do is replace an attribute that is in a (tabindex) child div. Is there any function where I can change this variable, without having to give each one.

Inside this variable I have 2 tabindex, in which I have to change.

var div = '
<div class="teste">
    Meu teste
    <div class="filho" tabindex="1">
       Div filho
       <input type="text" class="search" tabindex="2" />
    </div>
</div>';

ex:
.after(function(){
  return $(div)....attr("tabindex","10");
})
    
asked by anonymous 13.05.2015 / 02:31

1 answer

2

Convert the string to a DOM element, which you can traverse normally:

var teste = $(div);
teste.find('.filho').attr('tabindex', 10);
teste.find('.search').attr('tabindex', 20);

// Depois você pode usar o nó criado normalmente:
elemento.after(teste);
    
13.05.2015 / 03:01