#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    38
    Rep Power
    2

    Passing variable in javascript


    Hello all. I am writing an app using html5, js and css. I have run into a problem passing a value to a function. Seems like it would be straight forward so I am not sure why I am running into this.

    I query a database using javascript and display the data using the following code... (all of the data is displayed correctly on the page)

    Code:
    $('#page3 article').html('<p><strong>' + row.title + '</strong>.......more code......<br><a href="#pagereview2" data-trnote="' + row.id2 + '">Read Review</a></p>' );

    I send row.id2 to the page #pagereview. This page calls the function below.....


    Code:
    function getTitles14(id2) {

    Either my sql statement is incorrect or row.id2 is not making it to the function because the following does not work...


    Code:
    db.transaction(function(t) { 
    t.executeSql('SELECT id2, r_star, r_title, r_name, r_state, r_review, r_date FROM notes WHERE id2 = ? ORDER BY r_date ASC', [id2], function(t, result) {

    However, when I enter a definite value for id2 in the WHERE clause it works, like this...


    Code:
    db.transaction(function(t) { 
    t.executeSql('SELECT id2, r_star, r_title, r_name, r_state, r_review, r_date FROM notes WHERE id2 = "80" ORDER BY r_date ASC', [id2], function(t, result) {

    Does anyone have an idea what I may be missing here? It's been bugging me for two full days.

    Thanks for all help!
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,169
    Rep Power
    184
    Originally Posted by cjackson11
    Either my sql statement is incorrect or row.id2 is not making it to the function because the following does not work...
    Originally Posted by cjackson11
    However, when I enter a definite value for id2 in the WHERE clause it works, like this...
    Then I would say... if you can statically insert a value, into a SQL variable and the jQuery function; does what it is supposed to do... you got a issue with your SQL.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    38
    Rep Power
    2
    I have other sql statements on other pages that are formatted the same way. This is why I am at a loss of what could be wrong? How can I verify that the value is being passed correctly to the function?
  6. #4
  7. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,169
    Rep Power
    184
    Originally Posted by cjackson11
    How can I verify that the value is being passed correctly to the function?
    Well..., if your passing a SQL variable and a static value of said variable; like so:
    Code:
    id2 = "80"
    in your SQL query and the getTitles14() function is getting the "id2" parameter and does with it (whatever you have it doing) in your function; then the second SQL statement:
    Code:
    db.transaction(function(t) { 
    t.executeSql('SELECT id2, r_star, r_title, r_name, r_state, r_review, r_date FROM notes WHERE id2 = "80" ORDER BY r_date ASC', [id2], function(t, result) {
    then this means, that your JavaScript function and your SQL query; are working correctly. That's why I would say, this issue is with this SQL statement:
    Code:
    db.transaction(function(t) { 
    t.executeSql('SELECT id2, r_star, r_title, r_name, r_state, r_review, r_date FROM notes WHERE id2 = ? ORDER BY r_date ASC', [id2], function(t, result) {
    Or... maybe there is an issue with how your passing the value to the dynamic "id2" variable (in your SQL statement above) and that's a possible issue, as well.
    Last edited by web_loone08; April 18th, 2013 at 01:04 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    38
    Rep Power
    2
    Hmm, I have similar SQL statements so I will have to compare this one carefully.

    Can you please elaborate on an issue with passing the dynamic value? Do you mean there may be an issue of passing it from the previous page?

    Thanks!
  10. #6
  11. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,169
    Rep Power
    184
    Originally Posted by cjackson11
    Can you please elaborate on an issue with passing the dynamic value? Do you mean there may be an issue of passing it from the previous page?
    The question mark in "id2 = ?" (in your SQL statement) is representative of a dynamic parameter. So, you would have to be passing that parameter to the "id2", in your SQL statement. How are you passing, that dynamic parameter to your SQL statement? I think that might be something to look at, in your code.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    38
    Rep Power
    2
    Doesn't the following snippet bring the value into the function?

    Code:
    function getTitles14(id2) {
    I guess I need to make sure it is making it into the function. Is there a way I can verify that it has passed to the function correctly?

    Thanks.
  14. #8
  15. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,169
    Rep Power
    184
    Is "getTitles14" a JavaScript or a SQL function, because (from your code); I was under the impression, that it was a JavaScript function?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    38
    Rep Power
    2
    It is a javascript function
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    38
    Rep Power
    2
    Ok, I just tried to print the variable of id2 to see if it is making it to the function and it doesn't seem to be. The only thing that is displayed using the following code is [object Object]

    Code:
    function getTitles14(id2) {
        document.write (id2);

    This is the link I am using to pass the value to the function

    Code:
    <a href="#pagereview2" data-trnote="' + row.id2 + '">Read Review</a>
    Note that 'pagereview2' calls the getTitles14 function. Is there a different way I should pass the variable?
  20. #11
  21. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,169
    Rep Power
    184
    I guess that's my fault, for not asking; but I was under the impression, that you knew how to pass the parameter (in jQuery/JavaScript). Ok, here is a static example of how you do this, with a combination of jQuery and JS.
    Code:
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
    function getTitles14(id2) {
     alert(id2);
    }
    </script>
    
    <a class="pagereview2" href="#pagereview2" data-trnote="123">Read Review</a>
    
    <script>
    $(".pagereview2").click(function(){
       getTitles14(''+$(this).attr("data-trnote")+'');
       return false;
    });
    </script>
    You will have to insert (echo/print out each of your links and their attributes, with SQL and a Server Side Language of your choosing; but this should serve, as a basic working example. This way, you can see how to pass a parameter from jQuery to JavaScript.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    38
    Rep Power
    2
    Thanks so much for the example. Please forgive me for being so ignorant about this. Obviously this isn't my strong suit. I thank you very much for your patience with me.

    I have added the code...

    <a class="pagereview2" href="#pagereview2" data-trnote="123">Read Review</a>

    ..and placed this right below the above link...

    <script>
    $(".pagereview2").click(function(){
    getTitles14(''+$(this).attr("data-trnote")+'');
    return false;
    });
    </script>


    The alert box now shows the value in the getTitles14() function but it doesn't advance past the alert (even when the alert is closed.

    It does advance if I take the .pagereview2 script above out, but it obviously doesn't pass the value.


    I'm not sure if this helps but here is my code that includes the link to pagereview2...

    Code:
    $('#page3 article').html('<p><strong>' + row.title + '</strong><br>' + row.category + '<br><br>' + row.description + '<br><br>' + row.restaurant + '<br>' + row.address + '<br>' + row.city + ', ' + row.state + ' ' + row.zip + '<br>' + row.phone + '<br><a href="http://' + row.url + '" id="redlink">Website</a><br><a class="pagereview2" href="#pagereview2" data-trnote="123">Read Review</a></p>' );
                         
                         $(".pagereview2").click(function(){
                                                 getTitles14(''+$(this).attr("data-trnote")+'');
                                                 return false;
                                                 });
  24. #13
  25. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,169
    Rep Power
    184
    Code:
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
    
    // array made for demo purposes only - begin
    
    var row = [];
    row.title="The White House";
    row.category="Presidental";
    row.description="The United States of America";
    row.restaurant="The President's Choice";
    row.address="123 Main Street";
    row.city="Washington";
    row.state="DC";
    row.zip="20500";
    row.phone="202-456-1111";
    row.url="www.whitehouse.gov";
    
    // array made for demo purposes only - end
    
    function getTitles14(id2) {
     alert(id2);
    }
    
    $("document").ready(function(){
    $('#page3 .article').html('<p><strong>' + row.title + '</strong><br>' + row.category + '<br><br>' + row.description + '<br><br>' + row.restaurant + '<br>' + row.address + '<br>' + row.city + ', ' + row.state + ' ' + row.zip + '<br>' + row.phone + '<br><a href="http://' + row.url + '" id="redlink">Website</a><br><a class="pagereview2" href="#pagereview2" data-trnote="123">Read Review</a></p>');
    });
    
    $(window).load(function(){
     $(".pagereview2").click(function(){
       getTitles14(''+$(this).attr("data-trnote")+'');
       return false;
       });
    });
    
    </script>
    
    <!-- head end -->
    
    <!-- body begin -->
    
    <div id="page3">
    <div class="article">
    
    </div>
    </div>
    Given, what I am viewing (from what you display, that you understand about jQuery/JavaScript); it looks to me, like you need to do some online researching about "jQuery Events and Triggers" and "JavaScript Parameters". If you already know some basic jQuery/JS; I really think it would benefit you to take a refresher course on some of the above topics, that I have outlined.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    38
    Rep Power
    2
    Sorry for the delay in responding. I have been out of town. Thanks so much for the example. That helps a lot! You are right, I need to find a good refresher so I am more comfortable with this.

    Thanks for your patience and help!

IMN logo majestic logo threadwatch logo seochat tools logo