Avatar of Isaac
Isaac
Flag 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

JavaScriptjQuery

Avatar of undefined
Last Comment
Isaac

8/22/2022 - Mon
David H.H.Lee

Can i see your openModal() function?
Isaac

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

Open in new window

David H.H.Lee

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Vimal DM

Hi,

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

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Isaac

ASKER
Good Catch!  Thanks!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.