0

Hi I am trying to send two variables to jquery file for further processing from one form.

html form

<a href="#"><img class="<?php 
 if($favorite == 1) { 
  echo 'alreadyfavorite';
 } else { 
  echo 'addtofavorite';} ?>" 
id="addtofavoritebutton" pid="<?php $data['property_id']?>" 
fpid="<?php $favorite_properties_id;?>" src="../images/system/addtofavorite.png"></a>

jquery

$('#addtofavoritebutton').click(function() {
  var property_id = $('#addtofavoritebutton').attr("pid");
  var favorite_properties = $('#addtofavoritebutton').attr("fpid");
  alert(property_id);
  alert(favorite_properties);
});

but the values are not getting picked up by jquery...

9
  • Does the image exist when you add the event handler? Commented Jan 30, 2017 at 7:43
  • does it enter the handler at all (i.e.: does it fire some alert, although empty)? Commented Jan 30, 2017 at 7:44
  • @elementzero23 Can you provide documentation link where it states that .attr is deprecated? Commented Jan 30, 2017 at 7:44
  • yes it was firing empty at .attr(), and with .prop() it is firing undefined for both variables Commented Jan 30, 2017 at 7:45
  • 1
    I've rebuilt the thing and it seems to work correctly... Are you sure you have values in those attributes? Commented Jan 30, 2017 at 7:53

1 Answer 1

1

Use ready function and put you jquery code in ready function like:-

$(document).ready(function(){
    $('#addtofavoritebutton').click(function() {
      var property_id = $('#addtofavoritebutton').attr("pid");
      var favorite_properties = $('#addtofavoritebutton').attr("fpid");
      alert(property_id);
      alert(favorite_properties);
    });
})
Sign up to request clarification or add additional context in comments.

3 Comments

this is not working, the code above was already wrapped in $(document).ready(function(){ });
pid="<?php $data['property_id']?>" shoud be pid="<?php echo $data['property_id']?>"
it was not picking up data because echo is required

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.