m mov
m mov

Reputation: 99

jquery append not works in function

i have problem with append in this function:

    <!-- js code here -->
    <script>
        function newAssign(divID)
        {
            $("div[id='"+divID+"']").append("<p> hello! </p>");
        }
    </script>

php code - call newAssign with divID onChange select

     <?php foreach($myvar as $each): ?>
          <select name="weekday" class="innerComboStyle" onChange="newAssign(<?php echo $each->getId() ?>);" >
                <!-- <options here... -->
          </select>
    <div id="<?php echo $each->getId() ?>" ></div>
    <?php endforeach; ?>

I got NO ERRORs in firebug console but append not works still!

Upvotes: 0

Views: 95

Answers (2)

Andreas Wong
Andreas Wong

Reputation: 60516

You are missing quotes(') around the argument of newAssign call:

onChange="newAssign('<?php echo $each->getId() ?>')"

Unless getId() is a number or a string this, not putting ' won't work as javascript would think that you are passing a variable name. And even if it does work, it might not be as you intended.

You can instead do $('#' + divId).append(...), it's easier to look at rather than using id=

Upvotes: 3

Guffa
Guffa

Reputation: 700342

You need apostrophes around the id in the call, otherwise it will be interpreted as a variable name:

<select name="weekday" class="innerComboStyle" onChange="newAssign('<?php echo $each->getId() ?>');" >

Normally an id is unique in the page, and I suppose that you don't use an id that is not on a div, so you could just use the id in the selector:

$("#"+divID).append("<p> hello! </p>");

Upvotes: 0

Related Questions