Tsundoku
Tsundoku

Reputation: 9418

Element.text is not a function

I know this has been asked plenty of times before and I already went through a bunch of posts as well as googled for the answer but I just can't figure it out... here's my php

$connect = mysql_connect("localhost", $usuario, $password) or die(mysql_error());
$select_db = mysql_select_db($dbname) or die(mysql_error());

  //query the database
  $query = mysql_query("SELECT css_id, body FROM content");

    //loop through and return results
  for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {
    $row = mysql_fetch_assoc($query);
    $body[$x] = array("cssID" => $row["css_id"], "inlineContent" => $row["body"]);      
  }

  //echo JSON to page
  $response = $_GET["jsoncallback"] . "(" . json_encode($body) . ")";
  echo $response; 

my HTML:

<body>
<h2 class="inlineEdit" id="titulo">Editando</h2>
<div id="response"></div>
<ul>
  <li class="inlineEdit" id="linea">Lorem Ipsum....</li>
</ul>
</body>

and finally my jQuery:

$(function () {
    var domID = [];
    $(".inlineEdit").each(function(){ 
        domID.push(this.id);
    });

    $.getJSON("assets/php/load.php?jsoncallback=?", checkArray);

    function checkArray(data){
        for (var x = 0; x < data.length; x++){//loop through all items in the JSON array
            for(var j = 0; j < domID.length; j++){//loop through the DOM id's array
                if(domID[j] === data[x].cssID){
                    var Element = "$('#" + domID[j] + "')";
                    Element.text(data[x].inlineContent);
                }
            }
        }
    }
});

I've checked this using firebug and I know for sure that Element equals $('#linea') and data[x].inlineContent contains the correct data but I keep getting the same:

Element.text is not a function

message...

Upvotes: 1

Views: 1294

Answers (2)

rahul
rahul

Reputation: 187070

var Element = "$('#" + domID[j] + "')";

assigns a string to variable Element which don't have a text function. Also a syntax error. Thanks @o.k.w for pointing this out.

var Element = $('#' + domID[j] );

assigns jQuery object to variable Element

Upvotes: 0

o.k.w
o.k.w

Reputation: 25810

Should be:

var Element = $("#" + domID[j]);

else Element is a string.

Upvotes: 5

Related Questions