Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

onclick in anchor tag

How come my onclick isn't working below?

	function complete(data) 
	{		
		var bldRow=$('#Iblog').DataTable();
		

		for(var i =0; i < data.d.results.length; i++) 
		{		
					   //var rowNode = 
		    bldRow.row.add([ data.d.results[i].Created, "<a href='#' onclick='openModal();return false;'>"+data.d.results[i].Title+"</a>", data.d.results[i].NumCommentsId]).draw();				   
		}

		//$('#myBlogData').append(bldRow);
	 }
	 

Open in new window

Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Can i see your openModal() function?
Avatar of Isaac

ASKER

Nothing special...just an alert()
	 function openModal() {
alert("HELLO WORLD");
	 }

Open in new window

Do you get the result in the looping recordset below?

eg:
data.d.results[i].Created
data.d.results[i].Title
data.d.results[i].NumCommentsId]

var v1,v2,v3, rv2;

for(var i =0; i < data.d.results.length; i++) 
		{	
           v1=data.d.results[i].Created;
           v2=data.d.results[i].Title;
           v3=data.d.results[i].NumCommentsId;

           //alert(v1);  
           //alert(v2);  
           //alert(v3);
  
           rv2= "<a href='#' onclick='openModal();return false;'>"+v2+"</a>";

          bldRow.row.add([v1,rv2,v3]).draw();				   
}

Open in new window


If you can't get the result above, then you may replace this line:
 rv2= "&lt;a href='#' onclick='openModal();return false;'&gt;"+v2+"&lt;/a&gt;";

You may need to unveil more code for further inspection.
Hi,

I guess it might be the single quotes or double quotes which you have used to populate the tag link in the script.
Avatar of Isaac

ASKER

Maybe this will help.  Here is the whole javascript and html code in different files

$(document).ready(function(){
		$('#blog').DataTable();

		getListItems('http://www.mysite.com','Posts','complete', 'failure','timeout');
	});


function getListItems(url, listname, complete, failure, timeout) {

    // Executing our items via an ajax request
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items?select=Created,Title,NumCommentsId",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
		timeout: 2000,
		dataType:"json",
        success: function (data) {
            complete(data); // Returns JSON collection of the results
        },
        error: function (xhr,status,error) {
            console.log(status);
        }
    });
	
	function complete(data) 
	{		
		var bldRow=$('#Iblog').DataTable();
		

		for(var i =0; i < data.d.results.length; i++) 
		{		
					   //var rowNode = 
		    bldRow.row.add([ data.d.results[i].Created, "<a href='#' onclick='openModal();return false;'>"+data.d.results[i].Title+"</a>", data.d.results[i].NumCommentsId]).draw();				   
		}

		//$('#myBlogData').append(bldRow);
	 }
	 
	 function openModal() {
		alert("open modal");
	 }
	function failure(data) {
	 alert("Operation failed.");
	 }

}

Open in new window


HTML Code
<html>
<head>
	<title></title>
<style>
table.dataTable {
    width:auto !important;
}
</style>

</head>
<body>

<table id="Iblog" class="display nowrap dataTable dtr-inline" cellspacing="0">
	<thead>
		<tr>
			<th>Date</th>
			<th>Title</th>
			<th># of Comments</th>
		</tr>
	</thead>
	<!-- <tfoot>
		<tr>
			<th>Date</th>
			<th>Title</th>
			<th># of Comments</th>
			<th>Category</th>
		</tr> 
	-->
	</tfoot>
	<tbody id="myBlogData">

	</tbody>
</table>

</body>

</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaac

ASKER

Good Catch!  Thanks!