1

How can you use the :contain() on an input field, e.g.

#inputfield contains a dynamic value of 5.938 and I want to use $("#inputfield:contains('5.938')").

But that won't work, I've provided a demo at jsFiddle.

I don't want to use if(element.val() == '5.938') because I need a provisional selector instead doing an if/else statement.

I also don't mean by using [value=5.938] because the value will change.

4
  • 3
    :contains() doesn't work simply because it's not intended for use with the value attribute. Do you need to do stuff anytime the input's value becomes 5.938? Commented Jun 20, 2011 at 15:16
  • 2
    what is the scope which your code will run within (eg: a click handler, onchange, submit)? Commented Jun 20, 2011 at 15:16
  • Contains works for the value inside of an HTML dom element. not the value of an input element. See this fiddle: jsfiddle.net/maniator/ThFtt/2 Commented Jun 20, 2011 at 15:17
  • 1
    It would help if you could explain what the larger problem is that you're trying to solve. Commented Jun 20, 2011 at 15:23

5 Answers 5

4

Due to @Neal's answer, you need to use some sort of conditional. A simple selector won't work. .filter() will suffice, however:

$('#inputfield').filter(function ()
{
    return $(this).val() == '5.938';
});

That said, it looks like you're using an ID selector, which can select at most one element, so .filter() is overkill — if...else really makes the most sense in that case.

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

Comments

3

This should work:

$("#inputfield:text[value*='"+dynamicValue+"']").length

//will select text input #inputfield with a value containing dynamicValue.

jsfiddle: http://jsfiddle.net/XEP4c/3/

3 Comments

I don't understand what do you mean, but you can test with this jsfiddle: jsfiddle.net/XEP4c
its the same as doing $("#inputfield")
Actually much simpler and cleaner solution then the selected answer. I wish I could upvote more to promote it.
1

The answers here seem to get confusing as there isn't a clear explanation. When you set the value dynamically like:

$('#inputfield').val('5.938')

It does not work to get the value using the selectors like:

$('#inputfield[value="5.938"]')

There are a few workarounds for this. One is to use a filter:

var inputfield = $('#inputfield').filter(function () {
    return $(this).val() == '5.938';
});
if (inputfield.length) {
    alert("The input field has the value 5.938");
}

Or you could create your own selector:

(function($) {
    $.extend($.expr[':'], {
         val: function(el, i, attr) {
               return $(el).val() === attr[3];
         }
    });
}(jQuery));
if ($('#inputfield:val("5.938")').length) {
    alert("The input field has the value 5.938");
}

Comments

1

Manji's answer is simpler than the selected answer, although a simpler form again. I couldn't get this to work for me:

$("input[value='userID']").remove();

Concatenating the string to insert the dynamic variable works a treat instead though:

$("input[value='" + userID + "']").remove();

Obviously replace the .remove() with whatever you want to do with it.

Comments

-1

You want to use the attribute selector:

$('#inputfield[value='+dynamicValue+']')

7 Comments

Your jsFiddle example isn't even trying to select an input field; your jQuery selector that uses :contains is selecting a div. Maybe I'm missing what you're trying to do here? You can most definitely inject a dynamic value in an attribute selector; it has nothing to do with jQuery, it's basic JS string functionality.
@reko_t -- try it out -- it does not work, attributes are not dynamic, properties are
@reko_t it always shows 0 (i have no idea what browser shows otherwise)
@Neal: I'm using Firefox 4.0.1 on OS X and it always shows 1. Chrome 12.0.742.100 also shows 1 always. So does Safari Version 5.0.5 (6533.21.1). I'll be on Windows computer soon so I'll test my browser on it later and post results.
Hi, I just tested on FF 4.0.1 and Chrome 12.0.742.100 on Windows 7, and they both show 1. I don't know what's up with you getting different results. However with further testing if I wrap the value in quotes, then both FF and Chrome results 0. For example see: jsfiddle.net/pAWCQ The first alert is always 1, and the second alert is always 0, in both FF and Chrome.
|

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.