1

I have an anchor like this:

<a href="#" rel="1 4 7 18 ">Anchor</a>

Where 'rel' attribute values are ids of some items.
Than I have a form with an input, where user should type an id and click submit button.
On submit button click I need to check the value of input like this:

var value = $('a').attr('rel'); // get anchor 'rel' ids
var uservalue = $('input[type='text']').val();
if ( uservalue == '1' || uservalue == '4' || uservalue == '7' || uservalue == '18') {
// however I need the line above are created dynamically based on 'value' var
  alert('The id exists');
  return false;
} else {
  return true;
}

So, the question is how to create a line below dynamically based on anchor 'rel' attribute values?!
This is the line:

if ( value == '1' || value == '4' || value == '7' || value == '18') {
2
  • what do you mean by "how to create a line", what do you need to do? Commented Apr 21, 2010 at 8:00
  • I need just check if typed value by user doesn't exist in anchor rel ids. Commented Apr 21, 2010 at 8:21

4 Answers 4

1

you can create an array of values from the original rel value, and then check for a single value existence with inArray() method:

var values = $('a').attr('rel').split(' ');
alert ($.inArray(userSubmittedValue, values));
Sign up to request clarification or add additional context in comments.

Comments

1
var values = $('a').attr('rel').split(' ');

and now your values array will contain all the values. Loop through the array and do comparison.

Comments

0

I resolved the thing by myself:

var value = $('a').attr('rel'); // get anchor 'rel' ids
var uservalue = $('input[type='text']').val();

if ( value.match(uservalue) ) {
  alert('The id exists');
  return false;
} else {
  return true;
}

It was just necessary to put a match function.
Match function matches any result of a string, not the equal one.

value.match(uservalue)

The line above makes the following: searches any uservalue in value variable and match it.

Working example is placed here: http://jsfiddle.net/nYVX4/

1 Comment

In your example "2" will return "The id exists" because it matched "21".
0
var uservalue = $('input:text').val();

if ($('a[rel~="' + uservalue + '"]').length > 0) {
    alert('The id exists');
    return false;
} else {
    return true;
}

More: http://api.jquery.com/attribute-contains-word-selector/

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.