Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting value from span with jQuery

Tags:

html

jquery

Essentially what I'm trying to figure out is how to retrieve the value a span(id="camera_status") that's been populated with the name of a image after its been captured using phonegap.

The way I want it to work is that the image name will then be uploaded to the database along with the form information that's submitted. At the moment it seems to be reading the initial blank value before the span is populated with the image name. If there's anyone who know's how I can fix this i would be grateful.

HTML and Javascript of form page

<div>
  <input type="button" onclick="takePicture();" value="Take Picture" /><br/>
</div>
<div>
<div>
    <b>Status:</b> <span id="camera_status"></span><br>
    <b>Image:</b> <img style="width:240px;visibility:hidden;display:none;" id="camera_image" src="" />
</div>      
<div id="landmark-1" data-landmark-id="1">
<form id="uploadForm">
    <label for="email">
        <b>Email</b>
        <input type="email" id="email" name="email">
    </label>

    <label for="comment">
        <b>Comment</b></br>
        <input id="comment" name="comment" cols="30" rows="10"></textarea>
    </label>
    <input type="button" onclick="uploadPicture();" value="Upload New location" />  
    <input type="submit" value="Upload New location" />

    </form>

<script>
var spanValue = $('#camera_status').html()

$(function(){

$('#uploadForm').submit(function(){
    var landmarkID = $(this).parent().attr('data-landmark-id');
    var postData = $(this).serialize();

    $.ajax({
        type: 'POST',
        data: postData+'&lid='+spanValue,
        //change the url for your project
        url: 'save.php',
        success: function(data){
            console.log(data);
            alert('Your comment was successfully added');
        },
        error: function(){
            console.log(data);
            alert('There was an error adding your comment');
        }
    });

    return false;
 });
});

</div>
</div>
</div>
like image 867
user2236497 Avatar asked Dec 27 '22 04:12

user2236497


1 Answers

Try .text() :

var spanValue = $('#camera_status').text();
like image 77
Mohamed AbdElRazek Avatar answered Jan 16 '23 01:01

Mohamed AbdElRazek