3
<span class="field-validation-valid" data-valmsg-for="Classifications" data-valmsg-replace="true">
</span>

How can I add text to this span?

What I tried till now is the following:

var msg = $('#[data-valmsg-for="Classifications"]').val();
alert(msg);

Could some one throw idea on this.

2 Answers 2

11

Try this

$('span[data-valmsg-for="Classifications"]').text('Your Text'); //It wil add text to element

$('span[data-valmsg-for="Classifications"]').text();//It will get your element text

In your file get like this

var msg = $('span[data-valmsg-for="Classifications"]').text()
alert(msg);

DEMO

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

1 Comment

I got the below error while i tried with above code. Unhandled exception at line 1715, column 2 in ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js 0x800a139e - JavaScript runtime error: Syntax error, unrecognized expression: #[data-valmsg-for="Classifications"]
4

You need to use .text()

$("[data-valmsg-for='Classifications']").text("Some Text");

Demo


Note: Above selector will add text to ANY element having a custom attribute of data-valmsg-for with a value of Classifications, so if you want to be that specific, than use span[data-valmsg-for='Classifications']

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.