rogo rogo - 4 months ago 17
jQuery Question

ID ends with and appendchild / jquery / javascript

Hey i have problem with my code - can you look and solve my little problem?
I know that $ ( ) is not a function.
i can use getelementbyID but i need function ID ends with



<html>
<head>


</head>
<body>
<div id="pane_mrp:mrp">
asdsad
</div>

</body>
</html>

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
var mrp = document.getElementById('pane_mrp:mrp');
var ppostaci = document.createElement('div');
ppostaci.id = ppostaci;
var newHTML = "<div> bla bla bla</div>";
ppostaci.innerHTML = newHTML;
$("[id*='mrp']").appendChild(ppostaci);
</script>




Answer

You can use $("[id*='mrp']")[0] to get the normal JavaScript node element which works with .appendChild().

<html>
<head>
    

</head>
<body>
<div id="pane_mrp:mrp">
    asdsad
    </div>    
    
</body>
</html>

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
    var mrp = document.getElementById('pane_mrp:mrp');
    var ppostaci = document.createElement('div');
    ppostaci.id = ppostaci;
        var newHTML = "<div> bla bla bla</div>"; 
        ppostaci.innerHTML = newHTML;
        $("[id$='mrp']")[0].appendChild(ppostaci);
</script>

Note that $= is the ends-with selector.

You can also use the jQuery function .append():

$("[id$='mrp']").append(ppostaci)
Comments