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

Asked

Viewed 472 times

2

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

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");
})

1 answer

2


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

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);
  • I was wrong because it doesn’t put $(div) in a variable... Thank you.. ATT

Browser other questions tagged

You are not signed in. Login or sign up in order to post.