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

LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David H.H.LeeCommented:
Can i see your openModal() function?
IsaacSharePoint Client Side DeveloperAuthor Commented:
Nothing special...just an alert()
	 function openModal() {
alert("HELLO WORLD");
	 }

Open in new window

David H.H.LeeCommented:
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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Vimal DMSenior Software EngineerCommented:
Hi,

I guess it might be the single quotes or double quotes which you have used to populate the tag link in the script.
IsaacSharePoint Client Side DeveloperAuthor Commented:
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

hieloCommented:
try moving line 46 (the closing brace) to line 24 so that openModal() is defined globally.  The way you have it now, openModal() is defined within function getListItems(){...}, but the onclick handler is looking for a global definition of openModal:
$(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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IsaacSharePoint Client Side DeveloperAuthor Commented:
Good Catch!  Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.