0

I am wondering whether there is a way in which I can get the value within a given HTML element using JQuery?

Example

<span id="45463_test"></span>

(using the proper tags made the code to processed)

What I want is the '45463_test' value within the id attribute (using JQuery).

Thanx for the responses and the edit.

I would it such that I can get the attribute value of a tag which has been clicked.

1
  • I see in one of your comments that you only want the ID the tag that was clicked. Are you trying to get the ID from within a jQuery click handler? Commented Jul 9, 2010 at 9:41

4 Answers 4

2

You kind of need a different selector in order to tell jQuery which span element to look at.

$("span").attr("id")

Which would get the Id attr value of all spans on the page.

For example, if you had

<span id="mySpan" class="mySpanClass"></span>

you could get the IDs of all the spans with class "mySpanClass" using:

$("span[class='mySpanClass']").attr("id")

Documentation here: http://api.jquery.com/attr/

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

2 Comments

Thanx for the answer, now is there a way to sort through the data returned, since there are many [span] tags and I would only want the id of the tag which has been clicked?
$("span[class='mySpanClass']").attr("id") is not needed, Use: $("span.mySpanClass").attr("id")
1

Get: $("#45463_test").attr('id'); Set: $("#45463_test").attr('id','new value');

http://api.jquery.com/attr/

Comments

0

If you only want to get the ID of the <span/> you clicked then it's better to apply a click event handler to each span and catch the attribute value.

$("span").click(function() {
  return $(this).attr("id");
});

Comments

0

can't help but noticed,

you need something like this..

$("span").click(function() {
  alert(this.id);
});

good to play demo

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.