Function Allows Sending Only Integers and Not Strings on Click

Hello, when I try to send some data through variables on click, it allows only those with integer values and not strings. This has happened while I apply onclick() from a popup page.

Below is the scenario;

var user_id = 1;
var fname = "Erick";

<a href="#" onclick="setData('+user_id+')">Send</a> //**WORKS**

<a href="#" onclick="setData('+fname+')">Send</a> //**DOES NOT WORK**

//sendData function

function setData(getValue) {  ///-----When I user_id, everything works fine, When I send fname; no response
         alert(''+getValue+'');
}

From any experienced Javascript/JQuery developer; any help?

where is the code for that?

PS. your code uses two strings not string & integer.

Below is the exact code:

function getFundiDetails(user_id, fname, mname, lname, street, address, phone1, phone2, loc, experience, district, img, uci) {
    //JSON.stringify(data);
    // remove resultset if this has already been run
    $('.modal-content table').remove();
    $('.modal-content').empty();


    // add spinner to indicate something is happening
     $('<i class="fa fa-refresh fa-spin"/>').appendTo('#fundiDetails');

     var fundi_name = lname;

     var items = [];

     items.push('<table class="table-hover table-striped table-bordered"><tr><td class="genPads">\
                 <div class="list-item"><b>Name:</b> <span class"nameStyle">'+lname+' '+fname+'</span></div>\
                 <div class="list-item"><b>General Location:</b> '+loc+' <b>District:</b> '+district+'</div>\
                 <div class="list-item"><b>Address:</b> ' + address + '</div>\
                 <div class"list-item"><b>Street:</b> ' + street + '</div>\
                 <div class="list-item"><b>Experience:</b> ' + experience + '</div>\
                 <div class="list-item"><b>Contacts:</b> ' + phone1 + ' ' + phone2 + '</div></td>\
                 <td bg-color="white"><img src="' + main_uri + '/' + img + '" class="image_icon img-rounded" /></td></tr></table>');

     // remove spinner
     $('.fa-spin').remove();

     items.push('<div class="btn btn_fundi" align="center"><a href="#" onclick="postJobStart('+user_id+');">Start Work</a></div>');

            // append list to page
     $table = $('<table />').appendTo('.modal-content');
             //append list items to list
     $table.append(items);

     $('#myModal').modal('show');
}

//PostStartJob

window.postJobStart = function (data) {
    alert(''+data+'');
}

I don’t see anything in that code that relates to your issue …

Please note the following. When the below link is clicked as it’s OR with anything else that contains number values, no problem the postJobStart() function get reached with no issues.

Problem arises when the parsed variable let say:

var fname = “Trump”;

That way it throws an error: Uncaught ReferenceError: Trump is not defined at HTMLAnchorElement.onclick

Hope you get my point

Just an addition; postJobStart() is called from a Popup window.

you’re missing the string delemiters. see in your dev tools what HTML you actually create.

1 Like

Great idea, I was actually missing the delimiters, I had to define them again before posting; and gave them some delimiters as below

 var fname = "'" + fname + "'";
 var lname = "'"+lname+"'";

That worked fine. For numbers as you know, doesn’t matter whether there are delimiters or not, that’s why I was able to post number only. Thanks Dormilich.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.