0

I want to access the id of button , when i write the PHP variable the javascript code create error "Undefined" value.

function update_profile(){ $(function() {
var element = $(this);
var update_id = element.attr("id");
  var infoo = 'updated_id=' + update_id;

    document.getElementsByClassName('test')[0].innerHTML=infoo;


});
}

button code:

<button data-toggle="modal" data-target=".bs-example-modal-lg" onclick="update_profile()"  class="btn-xs btn-warning " id='<?php echo $user_rec[1]; ?>' >Update</button>
2
  • First of all I don't think you need $(function() { and closing it }); Commented Oct 7, 2017 at 17:23
  • Remove $(function() { }) and change document.getElementsByClassName('test')[0].innerHTML=infoo; to $(".test").first().html(infoo); or better: $(function() { $(".btn-warning").on("click",function() { $(".test").first().html(this.id); }); }); Commented Oct 7, 2017 at 17:23

1 Answer 1

1

You don't need $(function() { }); inside of your function. You have to pass this to your function so it knows which element was clicked.

function update_profile(element){
  var update_id = element.id;
  var infoo = 'updated_id=' + update_id;
  document.getElementsByClassName('test')[0].innerHTML = infoo;
}
<div class="test">test class element</div>
<button data-toggle="modal" data-target=".bs-example-modal-lg" onclick="update_profile(this)"  class="btn-xs btn-warning " id='idName' >Update</button>

Sign up to request clarification or add additional context in comments.

Comments

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.