0

I have a string that contains the following:

<form class="form" 
data-action="Create" 
data-entity="Topic" 
data-href="/Admin/Contents/JsonCreate" 
data-rowkey="007H" 
data-partitionkey="0006000" id="form">

How can I get the values of data-rowkey and data-partitionkey from this string and put into javascript variables?

Please note my data is not part of a page. This is still data in a string.

1
  • @Anne when you say it "didn't reach the browser" do you just mean it didn't reach the page yet, or that it might be running in some non-browser environment (e.g. node.js) Commented Oct 18, 2012 at 7:46

4 Answers 4

2

let formstring be the string containing your form declaration:

formstring.match(/data-rowkey=.+?"/i)[0].split('=')[1].replace(/"/g,'');
Sign up to request clarification or add additional context in comments.

Comments

0

if you are using jquery you can always go for

var data = $('.form').attr('data-rowkey);

2 Comments

Sorry the data is in a string. I just removed the jQuery tag. The data hasn't got close to the browser yet.
look at @Kooilnc answer, it should be sufficient
0
$("#form").attr("data-rowkey") 

1 Comment

Sorry - I just removed the jquery tag. My data is in a string. There is no browser involved yet.
0
var str = '<form class="form" data-action="Create" 
data-entity="Topic" data-href="/Admin/Contents/JsonCreate" 
data-rowkey="007H" data-partitionkey="0006000" id="form">';

var rowkey = str.match(/data-rowkey=(.+) data-partitionkey=(.+)/)[1];
var partition =str.match(/data-rowkey=(.+) data-partitionkey=(.+) id="form"/)[2];

FIDDLE DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.