Perocat
Perocat

Reputation: 1521

jQuery select all elements end with, not starting with

I thought this should select all elements ending with 1000_from but not those that starts with rif_. Where is the error?

$('[id$=1000_from]:not[id^=rif_]');

EDIT:

$('[id^=rif_][id$=_from]').on('keyup click change', function(){

    var rif_id = $(this).attr('id').split('_');

    var id = rif_id[1];
    var value = $(this).val();

    $('[id$='+id+'_from]:not([id^=rif_])').val(value);
});

It updates also the editing value

HTML:

Get value from:

<input type="datetime-local"
       name="rif_<?=$rif['p']?>_from"
       id="rif_<?=$rif['p']?>_from" />

Update to (there are many elements ending with <?=$rif['p']?>_from:

<input type="datetime-local"
       name="<?=$s['nr']?>_<?=$rif['p']?>_from"
       id="<?=$s['nr']?>_<?=$rif['p']?>_from" />

Upvotes: 0

Views: 43

Answers (1)

Kiran
Kiran

Reputation: 20313

You should put your condition in :not().

Replace :not[id^=rif_] with :not([id^="rif_"])

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

change id in your input field:

<input type="datetime-local"
       name="<?=$s['nr']?>_<?=$rif['p']?>_from"
       id="<?=$s['nr']?>_<?=$rif['p']?>_from" />

Try this:

$('[id$="1000_from"]:not([id^="rif_"])')

DEMO

Upvotes: 1

Related Questions