0

i have jquery string which have html ,

btn_response = '<input type="button" id="upload-btn5" style="margin-bottom: 7px;" class="btn btn-success clearfix" value="Choose file">';

the id attribute change every time , so i wonder how i can get id attribute value from that jquery string.

i tried

$(uploadBtn).data("id")

output

undefined

but no success !

can anybody help

1
  • 2
    jsfiddle.net/doatwc8s $(uploadBtn).data("id") will select attribute data-id and $(uploadBtn).attr("id") will select id attribute Commented Jul 15, 2015 at 7:41

1 Answer 1

2

You can wrap the string in jQuery and get the id of element.

$(btn_response).attr('id');

data() is used to get data-* attribute value. Use attr to get the id attribute value.

$(uploadBtn).data("id") will search for data-id attribute, which is not available so returns undefined.

Use:

$(uploadBtn).attr("id")

Demo:

var btn_response = '<input type="button" id="upload-btn5" style="margin-bottom: 7px;" class="btn btn-success clearfix" value="Choose file">';

alert($(btn_response).attr('id'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>

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.