babb
babb

Reputation: 423

javascript - Need to pull data out of a string

Hi I have a datatables based table that holds data. I need to be able to get the values out of the following string that I have been able to do so far.

<input name="jobNo" value="job_no_123" />job_no_123

I need to be able to get the name and the value and store into separate variables that I then pass off to do something else.

However I do also have another 4 fields on the page that I need to capture too:

<input name="item_1" value="data1" />data1
<input name="item_2" value="data2" />data2
<input name="item_3" value="data3" />data3
<input name="item_4" value="data4" />data4

And of top of this, this would only be the data from one row, and I need to do this for multiple rows too. But I need to start somewhere.

Please help.

Thanks

Upvotes: 0

Views: 77

Answers (2)

Kendall Frey
Kendall Frey

Reputation: 44374

This regex can find the values from your code. However, it assumes that the input is strictly in that format, with no variations in even whitespace.

<input name="(.*)" value="(.*)" />

The values will be in captured groups 1 and 2.

Note: I am aware that regex should not be used to parse HTML, but if the input is strict enough, it works.

Upvotes: 0

Hassek
Hassek

Reputation: 8995

I am not sure what are your needs but here is a code that should help

$('input').each(function(i, val){
    $(this).attr('name'); // will get the attribute name
    $(this).attr('value'); // will get the attribute value
    $(this).attr('text');  // will get the text inside the input
});

Upvotes: 1

Related Questions