Explodes in Jquery

Asked

Viewed 1,811 times

0

I’m still struggling in Jquery, before that I’m bringing a value of Mysql database:

<div id="cod_<?php echo $jmValor->IdCodUsuarios; ?>">Nome do usuário</div>

But I need to do type what happens with the PHP explode(), IE, remove the Cod_ and leave only the value coming from the bank. So I did so:

var id = valor.split('cod_');
var valor = document.getElementById(id);
$(".conteudoChat").load("chat/?convidar=s&Key="+valor);

What I need is to take the value from the database, put the value in the div.

Thank you!

1 answer

2


Long live!

I think this is what you want. Search for any div’s that have an "id" starting with "cod_" and enter in the contents of the div what is following the prefix "Cod_".

$('[id^="cod_"]').each(function() {
    $(this).text($(this).attr('id').split("_")[1]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="cod_0001"></div>
<div id="cod_0002"></div>
<div id="cod_0003"></div>
<div id="cod_0004"></div>
<div id="cod_0005"></div>

  • Hello Hélder. Thank you for the answer, but I still have a question. How would I get the value of div, since it is dynamic and put within the value variable?

  • @Jose.Marcos through an event onchange in the element.

  • I made a change to my post, which I think can solve your problem. However if you call the function in the onchange of the div as said @Renan should also work.

  • Thank you all for your help. It worked!

Browser other questions tagged

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